Java SQLite 更新查询 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21163313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLite update query Android
提问by user3199577
The table contains 4 columns : rowID , word , defintition , group_id
该表包含 4 列:rowID、word、defintition、group_id
I want to change a certain row's word and definition . So here is my code (word is an object where the word , definition , id and the group_id are stored)
我想更改某一行的单词和定义。所以这是我的代码(word 是一个对象,其中存储了 word 、 definition 、 id 和 group_id )
ContentValues values = new ContentValues();
ContentValues values = new ContentValues();
values.put(KEY_WORD, word.getWord());
values.put(KEY_DEFINITION, word.getDefinition());
db.update(TABLE_WORDS, values, KEY_ID, new String [] {String.valueOf(word.getID())});
Can anyone tell me why it only creates a new line instead of changing the row under given ID ?
谁能告诉我为什么它只创建一个新行而不是更改给定 ID 下的行?
回答by Digvesh Patel
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_DATE, contact.getDate());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
回答by Ahmad Raza
Use LIKE instead of = operator.
使用 LIKE 而不是 = 运算符。
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_DATE, contact.getDate());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " LIKE ?",
new String[] { String.valueOf(contact.getID()) });
回答by Mukesh Kumar Singh
You can try this using db.rawQuery
, i.e.
你可以试试这个使用db.rawQuery
,即
db.rawQuery("UPDATE "+ TABLE_WORDS + " SET "+ KEY_WORD + " = '"+word.getWord()+"' ,"+KEY_DEFINITION+"= '"+word.getDefinition()+ "' WHERE " + KEY_ID + " = "+word.getID(), null);
Here you don't need to create any ContentValues
.
在这里您无需创建任何ContentValues
.
回答by anddev84
@Digvesh has the right idea, but because of this limitation, converting an int to a String to use as a selection arg will not work properly. Instead do this:
@Digvesh 有正确的想法,但由于此限制,将 int 转换为 String 以用作选择 arg 将无法正常工作。而是这样做:
// assume: SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_WORD, word.getWord());
values.put(KEY_DEFINITION, word.getDefinition());
int rowsUpdated = db.update(TABLE_WORDS, values, KEY_ID + "=" + word.getID(), null);
回答by Fazal
String id = "1";
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NOTIFICATION_STATUS,"canceled");
db.update(TABLE_NOTIFICATION, values,COLUMN_NOTIFICATION_ID + " = ? ",new String[]{ String.valueOf(id) });
it will work as prepared statement. this piece of code worked in my case .. Thanks
它将作为准备好的声明工作。这段代码适用于我的情况..谢谢
回答by Muklas
this work for me, while db.rawquery didnot work.
这对我有用,而 db.rawquery 不起作用。
output query
输出查询
String qu="UPDATE "+ DATABASE_TABLE_DATA + " SET "+isbookmark+" = "+status+" WHERE id = "+uid+";";
db.execSQL(qu);
output query :
UPDATE tabel_data SET `isbookmark` = 1 WHERE id = 2720271;