Android数据库:如何将一列更新为另一列值? 例如,
Item
------------------------------------
id, name, suggestedPrice, salesPrice
将salesPrice更新为所有行的建议值,SQL:
update Item set salesPrice=suggestedPrice
在android中:
void execSQL(sql, bindingArg)
android文档说:执行一个不是SELECT / INSERT / UPDATE / DELETE的SQL语句。问题是execSql(..)不返回受影响的行数。还有其他解决办法吗?
答案 0 :(得分:1)
Android文档错误。理论上,您应该使用update() function,但是您不能使用SQL表达式来计算新值。
只需致电execSQL()
即可。要获取受影响的行数,请在之后调用changes():
db.execSQL("UPDATE ...", ...);
long rows = DatabaseUtils.longForQuery(db, "SELECT changes()", null);
答案 1 :(得分:0)
这是一个冗长的相对通用的解决方案。
基本上它使用SQLiteDatabase
update
方法从相应的行中获取要更新的值。
它已经变得安全,即SQL update Item set salesPrice=suggestedPrice
将更新所有行。
它只会根据id列更新一行。
public long testUpdate(SQLiteDatabase db,
String table,
String getcolumn,
String setcolumn,
String idcolumn,
Long id) {
long rv = -1;
boolean getcolumn_exists = false;
boolean setcolumn_exists = false;
boolean idcolumn_exists = false;
boolean update_value_obtained = false;
String updatevalue="";
Cursor chk1 = db.query(table,
null,null,null,null,null,null,
"1"
);
for (String col: chk1.getColumnNames()) {
if (col.equals(getcolumn)) {
getcolumn_exists = true;
}
if (col.equals(setcolumn)) {
setcolumn_exists = true;
}
if (col.equals(idcolumn)) {
idcolumn_exists = true;
}
}
chk1.close();
if (!getcolumn_exists || !setcolumn_exists || !idcolumn_exists) {
Log.d("TESTUPDATE","Missing Column(s) - Update not Attempted." +
" :Column " + getcolumn + " exists=" + String.valueOf(getcolumn_exists) +
" :Column " + setcolumn + " exists=" + String.valueOf(setcolumn_exists) +
" :Column " + idcolumn + " exists=" + String.valueOf(idcolumn_exists)
);
return rv;
}
Cursor chk2 = db.query(
table,
new String[]{getcolumn},
idcolumn + "=?",
new String[]{String.valueOf(id)},
null,null,null
);
if (chk2.moveToFirst()) {
updatevalue = chk2.getString(chk2.getColumnIndex(getcolumn));
update_value_obtained = true;
}
chk2.close();
if (!update_value_obtained) {
Log.d("TESTUPDATE","Value not obtained - Update not Attempted. " +
"Probably because ID " + String.valueOf(id) + " is not an existing column.");
return rv;
}
Log.d("TESTUPDATE", "Attempting Update of " + setcolumn + " with value=" + updatevalue);
ContentValues cv = new ContentValues();
cv.put(setcolumn,updatevalue);
rv = db.update(table,cv,idcolumn + "=?",new String[]{String.valueOf(id)});
Log.d("TESTUPDATE", "Updated " + String.valueOf(rv) + " rows.");
return rv;
}
该方法需要6个参数: -
进行相对广泛的检查: -
dbhlpr.testUpdate(
dbhlpr.getWritableDatabase(), //SQLiteDatabase
Datasource.TASKS_TABLE, // TABLE
Datasource.TASKS_EXPIRATION_COL,// COLUMN to get data from
Datasource.TASKS_NAME_COL, // Column to update
Datasource.TASKS_ID_COL, // ID Column
10L // The ID to identify the row
);
日志中的结果: -
01-11 04:28:35.366 1897-1897/? D/TESTUPDATE: Attempting Update of _name with value=31/12/2018
01-11 04:28:35.370 1897-1897/? D/TESTUPDATE: Updated 1 rows.
dbhlpr.testUpdate(
dbhlpr.getWritableDatabase(), //SQLiteDatabase
Datasource.TASKS_TABLE, // TABLE
"NOTACOLUMN",// COLUMN to get data from
"ANOTHENOTACOLUMN", // Column to update
"YETANOTHERNOTACOLUMN", // ID Column
10L // The ID to identify the row
);
日志中的结果: -
01-11 04:35:03.261 1967-1967/mjt.adapters D/TESTUPDATE: Missing Column(s) - Update not Attempted. :Column NOTACOLUMN exists=false :Column ANOTHENOTACOLUMN exists=false :Column YETANOTHERNOTACOLUMN exists=false
dbhlpr.testUpdate(
dbhlpr.getWritableDatabase(), //SQLiteDatabase
Datasource.TASKS_TABLE, // TABLE
Datasource.TASKS_EXPIRATION_COL,// COLUMN to get data from
Datasource.TASKS_NAME_COL, // Column to update
Datasource.TASKS_ID_COL, // ID Column
1123456789L // The ID to identify the row
);
日志中的结果: -
01-11 04:39:08.086 2030-2030/mjt.adapters D/TESTUPDATE: Value not obtained - Update not Attempted. Probably because ID 1123456789 is not an existing column.