Android 更新数据库';行不通
我正在安卓系统中发现数据库。我知道怎么读,但写不好。以下是数据库:Android 更新数据库';行不通,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我正在安卓系统中发现数据库。我知道怎么读,但写不好。以下是数据库: private static final String DATABASE_CREATE = "create table lesPlats (id integer primary key autoincrement, nom text not null, quantite integer);"; database.execSQL(DATABASE_CREATE); Con
private static final String DATABASE_CREATE = "create table lesPlats
(id integer primary key autoincrement, nom text not null, quantite integer);";
database.execSQL(DATABASE_CREATE);
ContentValues values = new ContentValues();
values.put("quantite", 1);
database.update("lesPlats", values, "id=?", new String[]{"1"});
我编辑它:
values.put("nom", "plat1");
values.put("quantite", 0);
database.insert("lesPlats", null, values);
我想增加“plat1”(注释中的调试版本)的数量:
通过调试,我得到了结果
之前的数量:0(id:1)
下一个数量:0(id:1)
所以在某个地方有个错误。你能告诉我密码是否正确吗?特别是这个:
database.rawQuery(“更新lesPlats SET quantite=1,其中id=“+n,null”)代码>
这意味着在id=1时数量被设置为1,不是吗?
我尝试使用UPDATE,得到了相同的结果…在调用第二个raw之前读取并关闭光标,因为没有它查询将无法执行:
c = database.rawQuery("UPDATE lesPlats SET quantite = 1 WHERE id = " + n, null);
c.moveToFirst();
c.close();
对不起,伙计们,我忘了在游标提示程序中设置数量
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很多
因此,有三种方法可以更改数据库:
private static final String DATABASE_CREATE = "create table lesPlats
(id integer primary key autoincrement, nom text not null, quantite integer);";
database.execSQL(DATABASE_CREATE);
ContentValues values = new ContentValues();
values.put("quantite", 1);
database.update("lesPlats", values, "id=?", new String[]{"1"});
或
或
pfiou 我忘了描述存储在数据库中的类:public class Comment{public long id;public String nom;public int quantite;不重要,您的问题是重复的。请参阅那篇文章中的解决方案。不过,您确实不应该使用rawQuery…请参阅此处,以获得更好的解决方案,我尝试使用values.put(“quantite”),2+cursorToComment(c).quantite);String Where=“id=?”;String[]wherergs=新字符串[]{String.valueOf(n)};database.update(TABLE_PLATS,values,Where,wherergs);没有任何更改您需要按照回答中的说明移动和关闭光标。我尝试您的代码和数据库。execSQL(“UPDATE lesPlats SET quantite=1,其中id=“+n”);但我没有更改。。。
c = database.rawQuery("UPDATE lesPlats SET quantite = 1 WHERE id = " + n, null);
c.moveToFirst();
c.close();