samedi 20 février 2016

update database android doesn't work

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...

Aucun commentaire:

Enregistrer un commentaire