Java Android 中的 SQLite 如何更新特定行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9798473/
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 in Android How to update a specific row
提问by EGHDK
I've been trying to update a specific row for a while now, and it seems that there are two ways to do this. From what I've read and tried, you can just use the:
一段时间以来,我一直在尝试更新特定行,似乎有两种方法可以做到这一点。根据我阅读和尝试的内容,您可以使用:
execSQL(String sql)
method
execSQL(String sql)
方法
or the:
或者:
update(String table, ContentValues values, String whereClause, String[] whereArgs)
method.
update(String table, ContentValues values, String whereClause, String[] whereArgs)
方法。
(Let me know if this is incorrect as I am new to android and very new to SQL.)
(让我知道这是否不正确,因为我是 android 新手并且对 SQL 非常陌生。)
So let me get to my actual code.
所以让我来看看我的实际代码。
myDB.update(TableName, "(Field1, Field2, Field3)" + " VALUES ('Bob', 19, 'Male')", "where _id = 1", null);
I am trying to accomplish this:
我正在努力实现这一目标:
Update Field1, Field2, and Field3 where the primary key (_id) is equal to 1.
更新 Field1、Field2 和 Field3,其中主键 (_id) 等于 1。
Eclipse gives me a red line right underneath the word "update" and gives me this explanation:
Eclipse 在“更新”这个词的正下方给了我一条红线,并给了我这样的解释:
The method update(String, ContentValues, String, String[]) in the type SQLiteDatabase is not applicable for the arguments (String, String, String, null)
SQLiteDatabase 类型中的方法 update(String, ContentValues, String, String[]) 不适用于参数 (String, String, String, null)
I'm guessing I'm not assigning the ContentValues correctly. Can anyone point me in the right direction?
我猜我没有正确分配 ContentValues。任何人都可以指出我正确的方向吗?
采纳答案by Akhil
First make a ContentValues object :
首先创建一个 ContentValues 对象:
ContentValues cv = new ContentValues();
cv.put("Field1","Bob"); //These Fields should be your String values of actual column names
cv.put("Field2","19");
cv.put("Field2","Male");
Then use the update method, it should work now:
然后使用更新方法,它现在应该可以工作了:
myDB.update(TableName, cv, "_id="+id, null);
回答by mcxiaoke
if your sqlite row has a unique id or other equivatent, you can use where clause, like this
如果您的 sqlite 行具有唯一的 id 或其他等效项,则可以使用 where 子句,如下所示
update .... where id = {here is your unique row id}
回答by Android
You try this one update method in SQLite
您在 SQLite 中尝试这种更新方法
int id;
ContentValues con = new ContentValues();
con.put(TITLE, title);
con.put(AREA, area);
con.put(DESCR, desc);
con.put(TAG, tag);
myDataBase.update(TABLE, con, KEY_ID + "=" + id,null);
回答by Yaqub Ahmad
Simple way:
简单的方法:
String strSQL = "UPDATE myTable SET Column1 = someValue WHERE columnId = "+ someValue;
myDataBase.execSQL(strSQL);
回答by Rahul Baradia
use this code in your DB `
在您的数据库中使用此代码`
public boolean updatedetails(long rowId,String name, String address)
{
ContentValues args = new ContentValues();
args.put(KEY_ROWID, rowId);
args.put(KEY_NAME, name);
args.put(KEY_ADDRESS, address);
int i = mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null);
return i > 0;
}
for updating in your sample.java use this code
要在您的 sample.java 中更新,请使用此代码
//DB.open();
try{
//capture the data from UI
String name = ((EditText)findViewById(R.id.name)).getText().toString().trim();
String address =(EditText)findViewById(R.id.address)).getText().toString().trim();
//open Db
pdb.open();
//Save into DBS
pdb.updatedetails(RowId, name, address);
Toast.makeText(this, "Modified Successfully", Toast.LENGTH_SHORT).show();
pdb.close();
startActivity(new Intent(this, sample.class));
finish();
}catch (Exception e) {
Log.e(TAG_AVV, "errorrrrr !!");
e.printStackTrace();
}
pdb.close();
回答by KarlKarlsom
- I personally prefere .update for its convenience. But execsql will work same.
- You are right with your guess that the problem is your content values. You should create a ContentValue Object and put the values for your database row there.
- 我个人更喜欢 .update 以方便使用。但 execsql 将工作相同。
- 您的猜测是正确的,问题在于您的内容价值。您应该创建一个 ContentValue 对象并将数据库行的值放在那里。
This code should fix your example:
此代码应该修复您的示例:
ContentValues data=new ContentValues();
data.put("Field1","bob");
data.put("Field2",19);
data.put("Field3","male");
DB.update(Tablename, data, "_id=" + id, null);
回答by sachi
hope this'll help you:
希望这会帮助你:
public boolean updatedetails(long rowId, String address)
{
SQLiteDatabase mDb= this.getWritableDatabase();
ContentValues args = new ContentValues();
args.put(KEY_ROWID, rowId);
args.put(KEY_ADDRESS, address);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null)>0;
}
回答by Satyam
just try this way
试试这个方法
String strFilter = "_id=" + Id;
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
myDB.update("titles", args, strFilter, null);**
回答by Jo?o Rosa
public long fillDataTempo(String table){
String[] table = new String[1];
tabela[0] = table;
ContentValues args = new ContentValues();
args.put(DBOpenHelper.DATA_HORA, new Date().toString());
args.put(DBOpenHelper.NOME_TABELA, nome_tabela);
return db.update(DATABASE_TABLE, args, STRING + " LIKE ?" ,tabela);
}
回答by Fracdroid
For updates, need to call setTransactionSuccessfull for changes to get committed like so:
对于更新,需要调用 setTransactionSuccessfull 以使更改提交,如下所示:
db.beginTransaction();
try {
db.update(...)
db.setTransactionSuccessfull(); // changes get rolled back if this not called
} finally {
db.endTransaction(); // commit or rollback
}