In my application I have a database. Although I can insert into the table successfully but I have problem in updating a field.
This code creates my table:
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" +
");";
I need that based on an event, set the status of item from “T” to “F” and vice versa.
For updating the database I have this code:
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.");
}
It seems everything is ok but I have no idea why — when I run the app and want to update, it crashes. Logcat shows these messages and points to 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)
Any comments or suggestion would be appreciated.
String literals must be enclosed in single quotes.
for example:
keep in mind that you and authenticated are column names.