lundi 2 mai 2016

How to add multiple array list values to the database in SQLite Android

I have 4 array lists containing values and i need to pass each of these 4 array lists values to 4 columns in the database at a time. When I tried loading them separately results are stored in a weird manner.

My code will be like,

Here "words" represent a string array list and I am adding it to the database while iterating the loop

   for (String fin : words) {
        System.out.println("the op is " + fin);
        boolean inserted = mydb.InsertAnsVal(fin);
        System.out.println("inserted");
        if (inserted == true) {
            Toast.makeText(MainActivity.this, "inserted", Toast.LENGTH_SHORT).show();
        }
    }

"Acroscluevalues" is another string array list and similarly the values will get inserted in the database . 'InsertCluesVal' is a method in database class

    for (String acrosclues : Acroscluevalues) {
        boolean inserted = mydb.InsertCluesVal(acrosclues);
        System.out.println("acros_clues_inserted");
        if (inserted == true) {
            Toast.makeText(MainActivity.this, "clue_inserted", Toast.LENGTH_SHORT).show();
        }
    }

When I insert it in my database, the results are stored like,

ID        WORDS             ACROSSCLUEVALUES
1           apple            
2           banana
3           orange
4                               rose
5                               sunflower

Here apple, banana, orange are the values of first array list and rose, sunflower are the values of second array list. I just showed the sample of 2 array list whereas in real, I have 4 array list. And I have primary key as

 ID INTEGER PRIMARY KEY

I didn't give as 'auto_increment' here but how is it incrementing the values of ID ?

My insert query will be like

 db.insertWithOnConflict(TABLE_NAME, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);                            

EXPECTED RESULT :

ID        WORDS             ACROSSCLUEVALUES
1           apple             rose
2           banana            sunflower
3           orange

Could any of you please help to store values properly !! Any help would be glad!! Thanks

Aucun commentaire:

Enregistrer un commentaire