Android sqlite 数据库更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10978136/
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 db update
提问by harsh
Is there an easy way to update a table in sqlite in android? (like a single line in built method) ? I have a table with few columns and primary is one column. I want to search by the primary key and then update a row in the table.
有没有一种简单的方法可以在android中的sqlite中更新表?(就像内置方法中的一行)?我有一个几列的表,主要是一列。我想按主键搜索,然后更新表中的一行。
采纳答案by GAMA
You can use rawQuery
like this:
你可以这样使用rawQuery
:
cur = mDb.rawQuery("update " + TABLE_NAME
+ " set column1=mango where id='" + _id + "'",null);
where
在哪里
cur
isCursor
objectTABLE_NAME
isNAME OF THE TABLE
_id
isname of the column
(only example)
cur
是Cursor
对象TABLE_NAME
是NAME OF THE TABLE
_id
是name of the column
(仅示例)
回答by waqaslam
To use with predefined updatemethod from android, use it as below:
要与来自 android 的预定义更新方法一起使用,请按如下方式使用它:
ContentValues args = new ContentValues();
args.put("col_name", "new value");
db.update("table_name", args, String.format("%s = ?", "primary_column"),
new String[]{"primary_id"});
Or to run as a single line, go with this (not recommended):
或者作为单行运行,使用这个(不推荐):
db.execSQL("UPDATE table_name SET col_name='new_value' WHERE
primary_column='primary_id'");
回答by Moog
Read the documentation for SQLiteDatabase.update
You should end up with something like this:
你应该得到这样的结果:
affected = db.update(TABLE_NAME, values, where, whereArgs);
affected = db.update(TABLE_NAME, values, where, whereArgs);
UDPATE
UDPATE
Avoid raw queries using error-prone syntax at all costs. I see a lot of answers here that use a lot of '"' + SOMETHING + "'"
... this is extremely bad practice and you will spend all your time looking for errors on places that are hard to find or simply a complete waste of time.
不惜一切代价避免使用容易出错的语法进行原始查询。我在这里看到很多答案都使用了很多'"' + SOMETHING + "'"
......这是非常糟糕的做法,您将花费所有时间在难以找到或完全浪费时间的地方寻找错误。
If you must use raw queries, try forming them with String.format to avoid perilous debug sessions and migraines.
如果您必须使用原始查询,请尝试使用 String.format 形成它们以避免危险的调试会话和偏头痛。
回答by RobGThai
Then you should already know what's your primary key.
那么你应该已经知道你的主键是什么了。
dbHelper.getWritableDatabase();
ContentValues values = createContentValues(profileVo);
db.update(ProfileVO.TABLE_NAME, values, ProfileVO.COLUMN_ID + "=" + profile.getId(), null)
Here's a good tutorial for you http://www.vogella.com/articles/AndroidSQLite/article.html
这是一个很好的教程http://www.vogella.com/articles/AndroidSQLite/article.html
回答by pawelzieba
回答by b.i
Try this:
尝试这个:
public void updateFunction(int id) {
String updateStmnt = "UPDATE YOUR_TABLE SET YOUR_COLUMN = "
+ id;
database.execSQL(updateStmnt);
}
Hope it will help.
希望它会有所帮助。
回答by Nadhir Titaouine
Using database.update
make it simple like this:
使用database.update
使它变得简单,如下所示:
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NAME, name);
values.put(MySQLiteHelper.COLUMN_JOB, job);
values.put(MySQLiteHelper.COLUMN_DATE_START, date_start);
database.update(MySQLiteHelper.TABLE_EMPLOYEES, values, MySQLiteHelper.COLUMN_ID+"="+id, null);
回答by Mazen el Senih
I know this a bit old, but in case anyone needed another way:
我知道这有点旧,但万一有人需要另一种方式:
public boolean updateNote(Note note) {
SQLiteDatabase db = notesDbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NotesDbContract.NoteEntry._ID, note.getId());
contentValues.put(NotesDbContract.NoteEntry.COLUMN_NAME_TITLE, note.getTitle());
contentValues.put(NotesDbContract.NoteEntry.COLUMN_NAME_DSECRIPTION, note.getDescription());
int result = db.update(NotesDbContract.NoteEntry.TABLE_NAME,
contentValues,
NotesDbContract.NoteEntry._ID + "=?", new String[]{String.valueOf(note.getId())}
);
db.close();
return result > 0;
}