Android SQLite Update语句不工作
我有一条update语句正在尝试执行:Android SQLite Update语句不工作,android,sqlite,Android,Sqlite,我有一条update语句正在尝试执行: String update_stock = "UPDATE " + table_name_ + " SET " + current_stock + " = " + current_stock + " - " + sub_stock + " where item_name = '" + item_name + "'"; getWritableDatabase().rawQuery(update_stock, null); 我已经检查了loglog.
String update_stock = "UPDATE " + table_name_ + " SET " + current_stock + " = " + current_stock + " - " + sub_stock + " where item_name = '" + item_name + "'";
getWritableDatabase().rawQuery(update_stock, null);
我已经检查了loglog.d(“updatesss”,update\u stock)中的查询代码>这给了我:
UPDATE APPLE\u CHILLAM SET current\u stock=current\u stock-1,其中item\u name='Double APPLE'
未更新该值。我错过了什么?
current\u stock
列的数据类型为int.使用execSQL
而不是rawQuery
:
execSQL("UPDATE " + table_name_ + " SET " + current_stock + " = " + current_stock - sub_stock + " where item_name = '" + item_name + "'");
execSql
与rawQuery
之间的差异:
执行一条SQL语句,该语句不是SELECT或任何其他返回数据的SQL语句
它无法返回任何数据(例如受影响的数量)
行)。相反,我们鼓励您使用insert(String,String,
ContentValues),更新(String,ContentValues,String,String[]),等等
艾尔,如果可能的话
运行提供的SQL并返回结果集上的游标
试着这样做:
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 + "'");
你确定有一行有
item\u name
'Double Apple'
为什么不使用ID?我的意思是id=‘某物’?是的,VladMatvienkoits是一样的。不会有重复的@3iLI建议您在某些DB浏览器中运行此查询。我使用DB browser for SQLite来实现这些目的。谢谢。唯一的区别是execSQL。我不明白为什么我没有work@TuhinSubhra草率的错误@TuhinSubhra,当您想要返回SELECT语句中的任何数据时,通常使用rawQuery。在将来的开发中,最好记住这一点。rawQuery有什么问题吗。你的建议奏效了。谢谢你,Godotwelocme,TuhinSubhra!我编辑了我的答案,看到这些链接,你就会知道不同之处了@Godot解释得很好