SQLite更新语句不起作用

时间:2018-02-01 07:34:07

标签: android sqlite

我有一个我正在尝试执行的更新声明:

String update_stock = "UPDATE " + table_name_ + " SET " + current_stock + " = " + current_stock + " - " + sub_stock + " where item_name = '" + item_name + "'";
    getWritableDatabase().rawQuery(update_stock, null);

我已经在日志Log.d("updatesss", update_stock);中检查了查询,该查询给了我:

UPDATE APPLE_CHILLAM SET current_stock = current_stock - 1 where item_name = 'Double Apple'

该值未获得更新。我错过了什么? current_stock列的数据类型为int。

2 个答案:

答案 0 :(得分:1)

使用execSQL代替rawQuery

execSQL("UPDATE " + table_name_ + " SET " + current_stock + " = " + current_stock - sub_stock + " where item_name = '" + item_name + "'");

execSqlrawQuery之间的差异

execSql

执行一个非SELECT的SQL语句或任何其他返回数据的SQL语句。

  

它无法返回任何数据(例如受影响的数量)   行)。相反,我们鼓励你使用insert(String,String,   ContentValues),update(String,ContentValues,String,String [])等   al,如果可能的话。

rawQuery

  

运行提供的SQL并在结果集上返回一个Cursor。

答案 1 :(得分:1)

尝试这样的事情:

SQLiteOpenHelper dbHelper = new DBHelper(this);
SQLiteDatabase database = dbHelper.getWritableDatabase();

database.execSQL("UPDATE " + table_name_ + " SET " + current_stock + " = " + current_stock + " - " + sub_stock + " where item_name = '" + item_name + "'");
相关问题