我控制favorites
的功能运行良好,但在LG G2这样的设备上我遇到no such column
错误,
我的代码:
public boolean adjustFavorites(Info info) {
String format = "INSERT INTO %s VALUES(%d,'%s','%s','%s');";
String deleteFormat = "DELETE FROM %s WHERE %s=%d ;";
long id = info.getId();
if (!isExist(id)) {
String query = String.format(format, TABLE_FAVORITE, id, info.getContent(), info.getTitle(), info.getParent());
database.execSQL(query);
} else {
String query = String.format(deleteFormat, TABLE_FAVORITE, DB_COL_ID, id);
database.execSQL(query);
}
return true;
}
private boolean isExist(long id) {
String format = "SELECT * FROM %s WHERE %s=%d ;";
String query = String.format(format, TABLE_FAVORITE, DB_COL_ID, id);
Cursor cursor = database.rawQuery(query, null);
if (cursor != null && cursor.moveToFirst())
if (cursor.getCount() > 0)
return true;
return false;
}
错误:
11-14 20:47:28.916 29227-29227/ir.mostafa.book E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: ir.mostafa.book, PID: 29227
android.database.sqlite.SQLiteException: no such column: ۱۴۱۰۸۷۶۰۹۷۸۰۷ (code 1): , while compiling: SELECT * FROM favorites WHERE id=۱۴۱۰۸۷۶۰۹۷۸۰۷ ;
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:892)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:503)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1415)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1354)
at ir.mostafa.book.db.DataBase.isExist(DataBase.java:177)
at ir.mostafa.book.db.DataBase.adjustFavorites(DataBase.java:162)
at ir.mostafa.book.activities.ContentViewer.onContextItemSelected(ContentViewer.java:279)
at android.app.Activity.onMenuItemSelected(Activity.java:2664)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:4037)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1158)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2957)
at android.widget.AbsListView$3.run(AbsListView.java:3849)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
问题不在于如何插入值,而在于如何构建SQL查询。您正在使用String.format
,它会对字符串进行本地化。不是格式化字符串,为什么不使用参数化查询:
Cursor cursor = database.query(TABLE_FAVORITE, null, DB_COL_ID+"=?",
new String[] { String.valueOf(id) }, null, null, null, null);