在我的应用程序中,我有一个数据库。虽然我可以成功插入表中,但我在更新字段时遇到问题。
此代码创建我的表:
public final static String[] TABLES_NAME = {"general", "toprecipes", "favorites", "shoppinglist"};
//-- Columns of table: shopping list
public final static String[] CLM_SHOPPINGLIST = {COLUMN_ID, "resepiId", "resepiName", "ingredient", "status"};
private final String CRT_TBL_SHOPPINGLIST = "CREATE TABLE " + TABLES_NAME[3] + " (" +
CLM_SHOPPINGLIST[0] + " INTEGER PRIMARY KEY AUTOINCREMENT," +
CLM_SHOPPINGLIST[1] + " TEXT," +
CLM_SHOPPINGLIST[2] + " TEXT," +
CLM_SHOPPINGLIST[3] + " TEXT," +
CLM_SHOPPINGLIST[4] + " TEXT" +
");";
我需要根据事件,将项目状态从“T”设置为“F”,反之亦然。
为了更新数据库,我有以下代码:
public void updateRecordStatus(String resepiName, String ingredientName, String status) {
ContentValues cv = new ContentValues();
cv.put(DatabaseHelper.CLM_SHOPPINGLIST[4], status);
database.update(DatabaseHelper.TABLES_NAME[3], cv, DatabaseHelper.CLM_SHOPPINGLIST[2] + "=" + resepiName + " AND " + DatabaseHelper.CLM_SHOPPINGLIST[3] + "=" + ingredientName, null);
Log.i(TAG, "Status of ingredient '" + ingredientName + "' updated to '" + status + "' in table.");
}
似乎一切正常,但我不知道为什么 - 当我运行应用程序并想要更新时,它会崩溃。 Logcat显示这些消息并指向database.update(...)
:
08-07 08:08:03.289: I/Database(460): sqlite returned: error code = 1, msg = near "Bakar": syntax error
08-07 08:08:03.289: E/Database(460): Error updating status=T using UPDATE shoppinglist SET status=? WHERE resepiName=Terung Bakar Mediterrinean AND ingredient=2 biji lemon
08-07 08:08:03.289: W/dalvikvm(460): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-07 08:08:03.323: E/AndroidRuntime(460): FATAL EXCEPTION: main
08-07 08:08:03.323: E/AndroidRuntime(460): android.database.sqlite.SQLiteException: near "Bakar": syntax error: , while compiling: UPDATE shoppinglist SET status=? WHERE resepiName=Terung Bakar Mediterrinean AND ingredient=2 biji lemon
08-07 08:08:03.323: E/AndroidRuntime(460): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
08-07 08:08:03.323: E/AndroidRuntime(460): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
08-07 08:08:03.323: E/AndroidRuntime(460): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
08-07 08:08:03.323: E/AndroidRuntime(460): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
08-07 08:08:03.323: E/AndroidRuntime(460): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
08-07 08:08:03.323: E/AndroidRuntime(460): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
08-07 08:08:03.323: E/AndroidRuntime(460): at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1671)
08-07 08:08:03.323: E/AndroidRuntime(460): at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1622)
08-07 08:08:03.323: E/AndroidRuntime(460): at com.astro.recipe.database.DBShoppingListHandler.updateRecordStatus(DBShoppingListHandler.java:107)
08-07 08:08:03.323: E/AndroidRuntime(460): at com.astro.recipe.activities.ShoppingList$2.OnCheckChangedListener(ShoppingList.java:90)
任何意见或建议都将不胜感激。
答案 0 :(得分:1)
字符串文字必须用单引号括起来。
例如:
Where you='jox' AND authenticated='1'
请记住,您和经过身份验证的是列名。
答案 1 :(得分:1)
替换
database.update(DatabaseHelper.TABLES_NAME[3], cv, DatabaseHelper.CLM_SHOPPINGLIST[2] + "=" + resepiName + " AND " + DatabaseHelper.CLM_SHOPPINGLIST[3] + "=" + ingredientName, null);
使用
database.update(DatabaseHelper.TABLES_NAME[3], cv, DatabaseHelper.CLM_SHOPPINGLIST[2] + "= '" + resepiName + "' AND " + DatabaseHelper.CLM_SHOPPINGLIST[3] + "='" + ingredientName + "'", null);
答案 2 :(得分:1)
database.update(DatabaseHelper.TABLES_NAME[3], cv, DatabaseHelper.CLM_SHOPPINGLIST[2] + "='" + resepiName + "' AND " + DatabaseHelper.CLM_SHOPPINGLIST[3] + "='" + ingredientName+"'", null);
尝试以上
为字符串添加单引号。
答案 3 :(得分:1)
变化:
database.update(DatabaseHelper.TABLES_NAME[3], cv,
DatabaseHelper.CLM_SHOPPINGLIST[2] + "=" + resepiName + " AND " +
DatabaseHelper.CLM_SHOPPINGLIST[3] + "=" + ingredientName, null);
要:
database.update(DatabaseHelper.TABLES_NAME[3], cv,
DatabaseHelper.CLM_SHOPPINGLIST[2] + " = ?" +
" AND " + DatabaseHelper.CLM_SHOPPINGLIST[3] + " = ?",
new String[]{resepiName, ingredientName});
这是使用带参数的sql语句的推荐方法(使用?
)