NOT NULL constraint failed: Facture.Mode - android

I tried to insert data into table Facture but I received a Crash report on a piece of code :"NOT NULL constraint failed: Facture.Mode (code 1299)".
I didn't find a way ti fix it. Could someone please help me?
There are my logcat.
07-22 13:59:01.565 2471-2471/com.example.pc.myapplication E/SQLiteDatabase: Error inserting U4=100 U1=100 A4=aa Q5=5 Q4=null A3=aaa A5=aa P5=100 P3=100 P4=100 U2=100 Q1=1 P2=100 A1=aa Q2=2 U5=100 Fournisseur=test Q3=3 U3=100 Datefactu=2/07/2018 Numero=123 A2=aaa Mode=null P1=100
android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: Facture.Mode (code 1299)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1474)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.example.pc.myapplication.DatabaseHelper.insertFacture(DatabaseHelper.java:178)
at com.example.pc.myapplication.facture1.onOKClick(facture1.java:103)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4720)
There is my class facture.java :
public void onOKClick ( View v )
{
if (v.getId() == R.id.buttonfacok) {
EditText numero = (EditText)findViewById(R.id.Etnum);
EditText date = (EditText)findViewById(R.id.Etdat);
EditText fournisseur = (EditText)findViewById(R.id.TVfour);
EditText modepaiement = (EditText)findViewById(R.id.TVmode);
EditText article1 = (EditText)findViewById(R.id.A1);
EditText article2 = (EditText)findViewById(R.id.A2);
EditText article3 = (EditText)findViewById(R.id.A3);
EditText article4 = (EditText)findViewById(R.id.A4);
EditText article5 = (EditText)findViewById(R.id.A5);
EditText quantite1 = (EditText)findViewById(R.id.Q1);
EditText quantite2 = (EditText)findViewById(R.id.Q2);
EditText quantite3 = (EditText)findViewById(R.id.Q3);
EditText quantite4 = (EditText)findViewById(R.id.Q4);
EditText quantite5 = (EditText)findViewById(R.id.Q5);
EditText unite1 = (EditText)findViewById(R.id.U1);
EditText unite2 = (EditText)findViewById(R.id.U2);
EditText unite3 = (EditText)findViewById(R.id.U3);
EditText unite4 = (EditText)findViewById(R.id.U4);
EditText unite5 = (EditText)findViewById(R.id.U5);
EditText prix1 = (EditText)findViewById(R.id.P1);
EditText prix2 = (EditText)findViewById(R.id.P2);
EditText prix3 = (EditText)findViewById(R.id.P3);
EditText prix4 = (EditText)findViewById(R.id.P4);
EditText prix5 = (EditText)findViewById(R.id.P5);
String strnumero = numero.getText().toString();
String strdate = date.getText().toString();
String strfournisseur = fournisseur.getText().toString();
String strmodepaiement = modepaiement.getText().toString();
String strarticle1 = article1.getText().toString();
String strarticle2 = article2.getText().toString();
String strarticle3 = article3.getText().toString();
String strarticle4 = article4.getText().toString();
String strarticle5 = article5.getText().toString();
String strquantite1 = quantite1.getText().toString();
String strquantite2 = quantite2.getText().toString();
String strquantite3 = quantite3.getText().toString();
String strquantite4 = quantite4.getText().toString();
String strquantite5 = quantite5.getText().toString();
String strunite1 = unite1.getText().toString();
String strunite2 = unite2.getText().toString();
String strunite3 = unite3.getText().toString();
String strunite4 = unite4.getText().toString();
String strunite5 = unite5.getText().toString();
String strprix1 = prix1.getText().toString();
String strprix2 = prix2.getText().toString();
String strprix3 = prix3.getText().toString();
String strprix4 = prix4.getText().toString();
String strprix5 = prix5.getText().toString();
Facture f = new Facture();
f.setNumero(strnumero);
f.setDatefactu(strdate);
f.setFournisseur(strfournisseur);
f.setMode(strmodepaiement);
f.setA1(strarticle1);
f.setA2(strarticle2);
f.setA3(strarticle3);
f.setA4(strarticle4);
f.setA5(strarticle5);
f.setQ1(strquantite1);
f.setQ2(strquantite2);
f.setQ3(strquantite3);
f.setQ4(strquantite4);
f.setQ5(strquantite5);
f.setU1(strunite1);
f.setU2(strunite2);
f.setU3(strunite3);
f.setU4(strunite4);
f.setU5(strunite5);
f.setP1(strprix1);
f.setP2(strprix2);
f.setP3(strprix3);
f.setP4(strprix4);
f.setP5(strprix5);
helper.insertFacture(f);
}
}
}
There is my databasehelper :
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4 ;
private static final String DATABASE_NAME = "contacts.db" ;
private static final String TABlE_NAME = "contacts" ;
private static final String COLUMN_ID = "id" ;
private static final String COLUMN_NOMETPRENOM = "nometprenom" ;
private static final String COLUMN_CIN = "cin" ;
private static final String COLUMN_MOTDEPASSE = "motdepasse" ;
private static final String TABlE_NAME1 = "Argent" ;
private static final String COLUMN_ENTREE = "Entree" ;
private static final String COLUMN_DATE = "date" ;
private static final String TABlE_NAME2 = "Facture" ;
private static final String COLUMN_NUMERO = "Numero" ;
private static final String COLUMN_DATEFOU = "Datefactu" ;
private static final String COLUMN_FOURNISSEUR = "Fournisseur" ;
private static final String COLUMN_MODE = "Mode" ;
private static final String COLUMN_Article1 = "A1" ;
private static final String COLUMN_Article2 = "A2" ;
private static final String COLUMN_Article3 = "A3" ;
private static final String COLUMN_Article4 = "A4" ;
private static final String COLUMN_Article5 = "A5" ;
private static final String COLUMN_Quantite1 = "Q1" ;
private static final String COLUMN_Quantite2 = "Q2" ;
private static final String COLUMN_Quantite3 = "Q3" ;
private static final String COLUMN_Quantite4 = "Q4" ;
private static final String COLUMN_Quantite5= "Q5" ;
private static final String COLUMN_Unitaire1 = "U1" ;
private static final String COLUMN_Unitaire2 = "U2" ;
private static final String COLUMN_Unitaire3 = "U3" ;
private static final String COLUMN_Unitaire4 = "U4" ;
private static final String COLUMN_Unitaire5 = "U5" ;
private static final String COLUMN_Prix1 = "P1" ;
private static final String COLUMN_Prix2 = "P2" ;
private static final String COLUMN_Prix3 = "P3" ;
private static final String COLUMN_Prix4 = "P4" ;
private static final String COLUMN_Prix5 = "P5" ;
SQLiteDatabase db;
private static final String TABlE_CREATE = "create table contacts ( id integer primary key not null , nometprenom Text not null , cin Text not null , motdepasse Text not null);" ;
private static final String TABlE_CREATE1 = "create table Argent ( id integer primary key not null , date Text not null , Entree Text not null);" ;
private static final String TABlE_CREATE2 = "create table Facture ( id integer primary key not null , Datefactu Text not null , Numero Text not null , Fournisseur Text not null , Mode Text not null , A1 Text not null , A2 Text not null , A3 Text not null , A4 Text not null , A5 Text not null , Q1 Text not null , Q2 Text not null , Q3 Text not null , Q4 Text not null , Q5 Text not null , U1 Text not null , U2 Text not null , U3 Text not null , U4 Text not null , U5 Text not null , P1 Text not null , P2 Text not null , P3 Text not null , P4 Text not null , P5 Text not null);" ;
public DatabaseHelper(Context context)
{
super(context ,DATABASE_NAME , null , DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABlE_CREATE2);
this.db=db;
}
public void insertFacture(Facture f)
{
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from Facture" ;
Cursor cursor = db.rawQuery(query , null) ;
int count = cursor.getCount();
values.put(COLUMN_NUMERO, f.getNumero());
values.put(COLUMN_DATEFOU , f.getDatefactu());
values.put(COLUMN_FOURNISSEUR , f.getFournisseur());
values.put(COLUMN_MODE, f.getMode());
values.put(COLUMN_Article1, f.getA1());
values.put(COLUMN_Article2, f.getA2());
values.put(COLUMN_Article3, f.getA3());
values.put(COLUMN_Article4, f.getA4());
values.put(COLUMN_Article5, f.getA5());
values.put(COLUMN_Quantite1, f.getQ1());
values.put(COLUMN_Quantite2, f.getQ2());
values.put(COLUMN_Quantite3, f.getQ3());
values.put(COLUMN_Quantite4, f.getQ4());
values.put(COLUMN_Quantite5, f.getQ5());
values.put(COLUMN_Unitaire1, f.getU1());
values.put(COLUMN_Unitaire2, f.getU2());
values.put(COLUMN_Unitaire3, f.getU3());
values.put(COLUMN_Unitaire4, f.getU4());
values.put(COLUMN_Unitaire5, f.getU5());
values.put(COLUMN_Prix1, f.getP1());
values.put(COLUMN_Prix2, f.getP2());
values.put(COLUMN_Prix3, f.getP3());
values.put(COLUMN_Prix4, f.getP4());
values.put(COLUMN_Prix5, f.getP5());
db.insert(TABlE_NAME2,null, values);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query2 = "DROP TABLE IF EXISTS "+TABlE_NAME2 ;
db.execSQL(query2);
this.onCreate(db);
}
}
Thanks in advance.

Mode's value as per Error inserting U4=100 U1=100 A4=aa Q5=5 Q4=null A3=aaa A5=aa P5=100 P3=100 P4=100 U2=100 Q1=1 P2=100 A1=aa Q2=2 U5=100 Fournisseur=test Q3=3 U3=100 Datefactu=2/07/2018 Numero=123 A2=aaa Mode=null P1=100
is null, you have defined the table so that it cannot be null as per .... , Mode Text not null , .....
You either need to pass a Facture object, to the insertFacture method, that provides, or results in a non-null value being used (which could be to only set the ContentValue for the column if Mode is not null) or you need to remove the not null constraint from the column's definition.
Re comment
#crammeur "Mode" must not be null . I declared as string but no result
When you declare a variable it's value will be null (unless it's a primitive type).
One get around could be to use the following as the insertFacture method :-
public void insertFacture(Facture f) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from Facture" ;
Cursor cursor = db.rawQuery(query , null) ;
int count = cursor.getCount();
values.put(COLUMN_NUMERO, f.getNumero());
values.put(COLUMN_DATEFOU , f.getDatefactu());
values.put(COLUMN_FOURNISSEUR , f.getFournisseur());
if (f.getMode != null ) {
values.put(COLUMN_MODE, f.getMode());
} else {
values.put(COLUMN_MODE,"No mode supplied");
}
values.put(COLUMN_Article1, f.getA1());
values.put(COLUMN_Article2, f.getA2());
values.put(COLUMN_Article3, f.getA3());
values.put(COLUMN_Article4, f.getA4());
values.put(COLUMN_Article5, f.getA5());
values.put(COLUMN_Quantite1, f.getQ1());
values.put(COLUMN_Quantite2, f.getQ2());
values.put(COLUMN_Quantite3, f.getQ3());
values.put(COLUMN_Quantite4, f.getQ4());
values.put(COLUMN_Quantite5, f.getQ5());
values.put(COLUMN_Unitaire1, f.getU1());
values.put(COLUMN_Unitaire2, f.getU2());
values.put(COLUMN_Unitaire3, f.getU3());
values.put(COLUMN_Unitaire4, f.getU4());
values.put(COLUMN_Unitaire5, f.getU5());
values.put(COLUMN_Prix1, f.getP1());
values.put(COLUMN_Prix2, f.getP2());
values.put(COLUMN_Prix3, f.getP3());
values.put(COLUMN_Prix4, f.getP4());
values.put(COLUMN_Prix5, f.getP5());
db.insert(TABlE_NAME2,null, values);
}
This would then use a value of No mode supplied should the value of the getMode method return null. Not necessarily the ideal fix though. The real fix would be ascertain why f.setMode(strmodepaiement); is returning null, which may well be due to the Facture's setMode method setting Mode to null (or not setting Mode at all).

Related

Java Android about Hashmap

I am trying to retrieve from UpdateProduct (Hashmap) to be able to modify my items by clicking the button. I implemented void modifyItem, but I see it is missing something.
File DataBase.java
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NOM = "Produits.db";
private static final String TABLE_PRODUCT = "Produits";
private static final String COLONNE_ID = "_id";
private static final String COLONNE_NAMEPRODUCT = "_nameProduct";
public void UpdateProduct(HashMap<String,String> object, String newValue) {
if(object == null) return;
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLONNE_NAMEPRODUCT, newValue);
int rows = db.update(TABLE_PRODUCT, values, "_id = ?", new String[]{object.get("id")});
db.close();
}
// MainActivity
Here my method to retrieve Hashmap data from UpdateProduct**
public void modifyItem(View view) {
if (objectSelected == null) return;
**????** selectedProduct = objectSelected.get("product");
if (selectedProduct != null && !selectedProduct.isEmpty()) {
DataBase.UpdateProduct(selectedProduct);
}
}`
Tryning to solve my project problems

how to turn two sqlite tables into a JSON object

Is it possible to turn a two table(relational tabel) from sqlite in to a JSON object? I've googling but still cannot find a way to convert those table. So far, i've only manage to turn one table into JSON object. If it's possible, can you tell me how to do it? if it's not, can you give me an alternatives? thanks.
here's the code that turn one table to JSON object:
private JSONArray getResults()
{
Context context = this;
String myPath = String.valueOf(context.getDatabasePath("ekantin1.db"));// Set path to database
String myTable = DatabaseHelper.ORDER_TABLE_NAME;//Set name of table
SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
String searchQuery = "SELECT * FROM " + myTable;
Cursor cursor = myDataBase.rawQuery(searchQuery, null );
JSONArray resultSet = new JSONArray();
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
int totalColumn = cursor.getColumnCount();
JSONObject rowObject = new JSONObject();
for( int i=0 ; i< totalColumn ; i++ )
{
if( cursor.getColumnName(i) != null )
{
try
{
if( cursor.getString(i) != null )
{
Log.d("TAG_NAME", cursor.getString(i) );
rowObject.put(cursor.getColumnName(i) , cursor.getString(i) );
}
else
{
rowObject.put( cursor.getColumnName(i) , "" );
}
}
catch( Exception e )
{
Log.d("TAG_NAME", e.getMessage() );
}
}
}
resultSet.put(rowObject);
cursor.moveToNext();
}
cursor.close();
Log.d("TAG_NAME", resultSet.toString() );
Intent pass_data = new Intent(this,BluetoothOut.class);
pass_data.putExtra("pindah",resultSet.toString());
startActivity(pass_data);
return resultSet;
}
}
And this is my table in my DatabaseHelper :
//tabel order
public static final String ORDER_TABLE_NAME="tb_order";
public static final String COL_1="ORDERID";
public static final String COL_2="USERID";
public static final String COL_3="PASSWORD";
public static final String COL_4="MEJA";
public static final String COL_5="TOPUP";
public static final String COL_6="SALDO";
//tabel lineitems
public static final String LINEITEMS_TABLE_NAME="tb_lineitems";
public static final String COL1 = "FOODID";
public static final String COL2 = "PRICE";
public static final String COL3 = "NUM";
public static final String COL4 = "RES";
public static final String COL6 = "ORDERID_FK";
table line items and orderid related to each other where orderid in tb_order as PK and orderid_fk in tb_lineitems as FK.
A good way to export data from your database is to use Gson, which is Google's Json serialization/deserialization library.
Fetch your Objects from your database like normally, and then use Gson to convert it into Json and export it.
Here is an example of how you could do it.
private void exportDatabase() {
// Create an instance of Gson.
Gson gson = new Gson();
// You can easily convert Objects into Json.
MyItem item = new MyItem();
String json = gson.toJson(item);
// Fetch your items from your database.
ArrayList<MyItems> items = database.getAll();
// Arrays are a bit harder to convert, but not very.
json = gson.toJson(items, new TypeToken<ArrayList<MyItems>>(){}.getType());
// Now export it to some easily copy-pasted location.
System.out.println(json);
}

Get String Array from Cursor

I have a SQLite Database with 45 different entries, each with:
public static final String TABLE = "Table";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_HOUR = "hour";
public static final String COLUMN_WEEK = "week";
public static final String COLUMN_DAY = "day";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_COLOUR = "colour";
public static final String COLUMN_ROOM = "room";
now I want to read out all. I Do this with following:
public Cursor fetchAllSubject(){
Cursor mCursor = database.query(true, TABLE, new String[] {
COLUMN_ID, COLUMN_HOUR, COLUMN_WEEK, COLUMN_DAY, COLUMN_NAME, COLUMN_DESCRIPTION, COLUMN_COLOUR, COLUMN_ROOM},null
, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
In a other class I have this code to read all out:
dao = new DAO(this);
Cursor subjectList = dao.fetchAllSubject();
Now I want to have for each entry an array with ID, Hour, week, ... but I have no idea how to do that.
My first try was following:
ArrayList<String> mo1h = new ArrayList<String>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_WEEK)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DAY)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_NAME)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DESCRIPTION)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_COLOUR)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ROOM)));
subjectList.moveToNext();
}
But everything is in mo1h, and I dont know how to devide it.
The best would be to have a String[] for each. Has anybody an Idea?
Thanks!
You can create on Bean class and then create one ArrayList (Collection class)
public class Bean
{
public Bean();
String id, hour, week, day, name, description, color, room;
}
now create list of Bean
ArrayList<Bean> mo1h = new ArrayList<Bean>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
Bean b = new Bean();
b.id = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID));
b.hour =subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR));
...
...
// all your column
mo1h.add(b);
}
Why not continue with your strategy, but instead use an ArrayList of String[]:
ArrayList<String[]> mo1h = new ArrayList<String[]>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
String[] toUse = new String[8];
toUse[0] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID));
toUse[1] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR));
toUse[2] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_WEEK));
toUse[3] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DAY));
toUse[4] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_NAME));
toUse[5] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DESCRIPTION));
toUse[6] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_COLOUR));
toUse[7] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ROOM));
mo1h.add(toUse);
subjectList.moveToNext();
}

Android : Sqlite Database insert?

How to insert a string array values to a table?
1,1,null,null,9876543210,null,1,-1,3,null,null,Testing message,null,0,0,0
The above is the value in a String[]. I want to insert this to my database table.
Here's my code : -
while((st = reader.readLine()) != null)
{
try
{
splitArray = st.split("\\n+");
}catch(PatternSyntaxException e ) { }
for(String result_array : splitArray)
{
Toast.makeText(context, result_array, Toast.LENGTH_SHORT).show();
}
What's the SQL statement to do this?
i have been trying to do the same thing and just worked it out. you
want to do something similar to this.
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
Code
public static final String ADDRESS = "address";
public static final String PERSON = "person";
public static final String DATE = "date";
public static final String READ = "read";
public static final String STATUS = "status";
public static final String TYPE = "type";
public static final String BODY = "body";
public static final int MESSAGE_TYPE_INBOX = 1;
public static final int MESSAGE_TYPE_SENT = 2;
ContentValues values = new ContentValues();
values.put(SMSHelper.ADDRESS, "+61408219690");
values.put(SMSHelper.DATE, "1237080365055");
values.put(SMSHelper.READ, 1);
values.put(SMSHelper.STATUS, -1);
values.put(SMSHelper.TYPE, 2);
values.put(SMSHelper.BODY, "SMS inserting test");
Uri inserted = getContentResolver().insert(Uri.parse("content:// sms"), values);
If you have a table with one column of type 'TEXT' and all you wish is to enter
those string but in one commit: then as far as I know you have to make an INSERT statement
for each of them, there is no way to insert all the string in only 1 INSERT statement (as long as you want each string to be a row in the table)
Read more

My device doesnt recognize my DB where my emulator does

When I try to query using my db in the femulator it works perfectly, but given the same appliction and same varibales into the device (milestone) it seems like the DB is not recognized.
I think the key to solving it is to understand where on the device we store the db.
On the emulator it's on /data/data/com.countryCityGame/databases/test1.db
But when I am using the device itself it must be in another place, does someone know where?
This is the java code where I build the DB:
public static void main(String[] args) throws Exception {
String CITIES[] = city.split(",");
String ANIMEL[] = animel.split(",");
String CELEB[] = UK_CELEB.split(",");
String Nature[] = vegtablesAndFruiets.split(",");
String V[][] = {COUNTRIES,CITIES,ANIMEL,Nature,occupations,CELEB};
String []TypeNames = {"Country","City","Animels","Nature","Occupation","Celeb"};
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:test1.db");
Statement stat = conn.createStatement();
//stat.executeUpdate("drop table if exists "+"android_metadata"+";");
stat.executeUpdate("create table android_metadata (\"locale\" TEXT DEFAULT 'en_US');");
//PreparedStatement prep1 = conn.prepareStatement(
// "insert into "+"android_metadata"+" values (?);");
//prep1.setString(1, "en_US") ;
//prep1.addBatch();
stat.executeUpdate("drop table if exists "+TABLE_NAME+";");
//stat.executeUpdate("create table "+TABLE_NAME+" (name, occupation);");
stat.executeUpdate("create table "+TABLE_NAME+" ("+VALUE+" TEXT NOT NULL,"+TYPE+" TEXT NOT NULL,"+LETTER+" TEXT NOT NULL,"+counter+" INTEGER);");
PreparedStatement prep = conn.prepareStatement(
"insert into "+TABLE_NAME+" values (?,?,?,?);");
//private void insertToTalble();
int j=0,i=0;
try{
for(j = 0 ;j < V.length; j++)
for (i = 0 ;i < V[j].length ; i++)
{
if (V[j][i] != null)
{
V[j][i] = asUpperCaseFirstChar(V[j][i]);
Character c = V[j][i].charAt(0);
prep.setString(3, c.toString());
}
prep.setString(1, V[j][i]);
prep.setString(2, TypeNames[j]);
prep.setInt(4,0);
prep.addBatch();
}
}catch(Exception e) {
System.out.println("***********"+i+"***************");
}
conn.setAutoCommit(false);
prep.executeBatch();
// prep1.executeBatch();
conn.setAutoCommit(true);
ResultSet rs = stat.executeQuery("select * from "+TABLE_NAME+";");
while (rs.next()) {
System.out.println("country name = " + rs.getString(VALUE));
System.out.println("type = " + rs.getString(TYPE));
System.out.println("letter = " + rs.getString(LETTER));
System.out.println("********************************");
}
rs.close();
conn.close();
}
and this is the code from where i first get the bug:
private static final String DB_PATH = "/data/data/com.countryCityGame/databases/test1.db";
private static final String DATABASE_NAME = "test1.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "GameTable";
private static final String VALUE = "value";
private static final String TYPE = "type";
private static final String LETTER = "letter";
public countryCityGameLogic(EditText[] myEditTextArr , Context context) {
points = 0;//init values
Cursor cursor = null ;
this.context = context;
openHelper = new OpenHelper(context);
gameList = new CharSequence [myEditTextArr.length];
for (int i = 0 ; i < myEditTextArr.length; i++){
gameList[i] = myEditTextArr[i].getText();
}
//this.db = openHelper.getWritableDatabase();
try{
db = SQLiteDatabase.openDatabase(DB_PATH , null, SQLiteDatabase.NO_LOCALIZED_COLLATORS/*SQLiteDatabase.CREATE_IF_NECESSARY*/);
//this question
cursor = db.query(TABLE_NAME, new String[] {LETTER}
,LETTER+ " like " + "'%" + "A" +"%'", null, null, null, null);
if ( !cursor.moveToFirst()){
//*****if data base doesnot exist HERE I GOT PROBLEMES****
this.db = openHelper.getWritableDatabase();//make new data base
}
}catch(SQLiteException e){
this.db = openHelper.getWritableDatabase();//make new data base
// for (int i = 0 ; i < ALLVALUES.length ; i++)
// insertValus(ALLVALUES[i],i);
}
NOTICE: In the device I am using /data/data/com.countryCityGame/databases/test1.db, it might not be the path where the appliction is located onto the device I am using.

Categories

Resources