0

i'm discovering database in Android. I understand how to read it but i don't succed writing it. Here is the database:

        private static final String DATABASE_CREATE = "create table lesPlats
             (id integer primary key autoincrement, nom text not null, quantite integer);";
        database.execSQL(DATABASE_CREATE);

i edit it:

        values.put("nom", "plat1");
        values.put("quantite", 0);
        database.insert("lesPlats", null, values);

i want to increase the quantity of "plat1" (debug version in comment):

        int n=1;
     // Cursor c = database.rawQuery("SELECT * FROM lesPlats WHERE id="+n, null);
     // c.moveToFirst();
     // System.out.println("QUANTITY BEFORE: "+cursorToComment(c).quantite+ " (id: "+cursorToComment(c).id+")");

        database.rawQuery("UPDATE lesPlats SET quantite = 1 WHERE id = " + n, null);

     // c = database.rawQuery("SELECT * FROM lesPlats WHERE id="+n, null);
     // c.moveToFirst();
     // System.out.println("QUANTITY NEXT: "+cursorToComment(c).quantite+ " (id: "+cursorToComment(c).id+")");

with debugging, i got the result

QUANTITY BEFORE: 0 (id: 1)

QUANTITY NEXT: 0 (id: 1)

So there is a mistake somewhere. Could you tell me if the code is good? specially this one:

database.rawQuery("UPDATE lesPlats SET quantite = 1 WHERE id = " + n, null);

This means that quantity is set to 1 at the id=1, isn't it? i try with UPDATE, i have the same result...

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
clo wan
  • 1
  • 3
  • i forgot to describe the class stored in the databse:public class Comment { public long id; public String nom; public int quantite; – clo wan Feb 20 '16 at 17:58
  • Not important, your question is a duplicate. See the solution in that post. – OneCricketeer Feb 20 '16 at 17:59
  • Though, you really shouldn't use rawQuery... See here for the better solution http://stackoverflow.com/q/3760774/2308683 – OneCricketeer Feb 20 '16 at 18:03
  • i try values.put("quantite", 2 + cursorToComment(c).quantite); String Where = "id=?"; String[] WhereArgs = new String[] {String.valueOf(n)}; database.update(TABLE_PLATS, values, where, whereArgs); there is no change – clo wan Feb 20 '16 at 18:51
  • You need to move and close the cursor as described in the answer – OneCricketeer Feb 20 '16 at 18:53

2 Answers2

0

read and close the cursor before calling the second raw, because the query won't get executed without it :

c = database.rawQuery("UPDATE lesPlats SET quantite = 1 WHERE id = " + n, null);
c.moveToFirst();
c.close();
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Boukharist
  • 1,219
  • 11
  • 19
  • i try your code and database.execSQL("UPDATE lesPlats SET quantite = 1 WHERE id = " + n); but i have no change... – clo wan Feb 20 '16 at 18:40
0

sorry guys, i've forgot the to set quantity in the cursorToComment procedure...

private Comment cursorToComment(Cursor cursor) {
    Comment comment = new Comment();
    comment.setId(cursor.getLong(0));
    comment.setNom(cursor.getString(1));
    comment.setQqte(cursor.getInt(2));   //here!!
    return comment;

thx a lot

So, three ways to change the database:

ContentValues values = new ContentValues();
values.put("quantite", 1);
database.update("lesPlats", values, "id=?", new String[]{"1"});

or

database.execSQL("UPDATE lesPlats SET quantite = 1 WHERE id = " + n);

or

c = database.rawQuery("UPDATE lesPlats SET quantite = 1 WHERE id = " + n, null);
c.moveToFirst();
c.close();

pfiou!

clo wan
  • 1
  • 3