我想在SQLite中更新一些数据。这是编辑数据的代码:
db.open();
c = db.getData();
if (c.moveToFirst()) {
do {
Log.v("_______BACK PRESSED", "______UUID___________"+c.getString(c.getColumnIndex("uuid")));
Log.v("_______pos", "______UUID___________"+pos);
String strSQL = "UPDATE DeviceDetails SET devicename ="+ edittext.getText().toString() +" WHERE uuid = "+c.getString(c.getColumnIndex("uuid")) ;
db.select(strSQL);
Log.v("_______BACK PRESSED", "______UUID___________"+c.getString(c.getColumnIndex("uuid")));
Log.v("_______BACK PRESSED", "______devicename___________"+c.getString(c.getColumnIndex("devicename")));
Log.v("___________text", "_______________"+c.getString(c.getColumnIndex("devicename")));
Log.v("___________edittext", "_______________"+edittext.getText().toString());
Log.v("_____ADDRESS______edittext", "_______________"+pos);
Intent intent=new Intent();
intent.putExtra("Dname", edittext.getText().toString());
Log.v("_____edittext in intent________", "__________"+edittext.getText().toString());
intent.putExtra("Daddress",pos);
Log.v("_____edittext in intent________", "__________"+pos);
setResult(RESULT_OK, intent);
finish();
} while (c.moveToNext());
}
db.close();
在数据库适配器中,我执行以下操作来执行查询。但它没有更新数据。
public Cursor select(String query) throws SQLException {
return db.rawQuery(query, null);
}
答案 0 :(得分:1)
SQL引用的字符串文字不正确''
。但是,最好的选择是使用?
占位符和绑定参数。
使用execSQL()
而非rawQuery()
进行更新。单独rawQuery()
就不会运行SQL; execSQL()
会。
示例:
db.execSQL("UPDATE DeviceDetails SET devicename = ? WHERE uuid = ?",
new String[] {
edittext.getText().toString(),
c.getString(c.getColumnIndex("uuid"))
});
答案 1 :(得分:0)
重命名简单更新
是什么意思?并且更新查询将在您执行此操作时执行此操作
只需使用此
更新您的查询String strSQL = "UPDATE DeviceDetails SET devicename ='"+ edittext.getText().toString() +"' WHERE uuid = '"+c.getString(c.getColumnIndex("uuid"))+"'" ;
答案 2 :(得分:0)
您应该使用.update
更新数据库中的行而不是.rawQuery
String value = c.getString(c.getColumnIndex("uuid"));
String[] whereVars = new String[]{value};
String where = "uuid = ?"
ContentValues args = new ContentValues();
args.put("devicename", edittext.getText().toString());
db.update("DeviceDetails", args, where, whereVars);
我还会告诉您,上述代码的性能,循环中的更新,将是废话..所以您应该在单个事务中执行所有更新:
db.beginTransaction();
try {
if (c.moveToFirst()) {
do {
updateDeviceByUuid(c.getString(c.getColumnIndex("uuid"),
edittext.getText().toString());
} while (c.moveToNext());
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
和
public int updateDeviceByUuid(String uuid, String deviceName){
String[] whereVars = new String[]{uuid};
String where = "uuid = ?"
ContentValues args = new ContentValues();
args.put("devicename", deviceName);
return db.update("DeviceDetails", args, where, whereVars);
}