我正在尝试从名为item_table
的数据库表中检索数据。我的列名为Category
,其中包含名为Pasta
的值。我正在尝试使用Where
子句检索数据,但我收到一个名为no such column as Pasta
的错误。请一步一步指导我。我的代码和错误日志如下:
错误日志
E/AndroidRuntime(4070): FATAL EXCEPTION: main
E/AndroidRuntime(4070): Process: com.alrimal, PID: 4070
E/AndroidRuntime(4070): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alrimal/com.alrimal.MenuItemsActivity}: android.database.sqlite.SQLiteException: no such column: Pasta (code 1): , while compiling: SELECT * FROM item_table WHERE Category=Pasta
E/AndroidRuntime(4070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
E/AndroidRuntime(4070): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
E/AndroidRuntime(4070): at android.app.ActivityThread.access$700(ActivityThread.java:135)
E/AndroidRuntime(4070): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
E/AndroidRuntime(4070): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(4070): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(4070): at android.app.ActivityThread.main(ActivityThread.java:4998)
E/AndroidRuntime(4070): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(4070): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(4070): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
E/AndroidRuntime(4070): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
E/AndroidRuntime(4070): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(4070): Caused by: android.database.sqlite.SQLiteException: no such column: Pasta (code 1): , while compiling: SELECT * FROM item_table WHERE Category=Pasta
E/AndroidRuntime(4070): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
E/AndroidRuntime(4070): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
E/AndroidRuntime(4070): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
E/AndroidRuntime(4070): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
E/AndroidRuntime(4070): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
E/AndroidRuntime(4070): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
E/AndroidRuntime(4070): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
E/AndroidRuntime(4070): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
E/AndroidRuntime(4070): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
E/AndroidRuntime(4070): at com.helper.DbHelper.retrieve_item_details(DbHelper.java:158)
E/AndroidRuntime(4070): at com.alrimal.MenuItemsActivity.onCreate(MenuItemsActivity.java:57)
E/AndroidRuntime(4070): at android.app.Activity.performCreate(Activity.java:5243)
E/AndroidRuntime(4070): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(4070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
E/AndroidRuntime(4070): ... 11 more
我的Dbhelper代码
public ArrayList<HashMap<String, String>> retrieve_item_details(String category) {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
//String selectQuery = "SELECT * FROM item_table";
String selectQuery = "SELECT * FROM item_table WHERE Category="+category;
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", cursor.getString(0));
map.put("id", cursor.getString(1));
map.put("Item_Id", cursor.getString(2));
map.put("Item_Name", cursor.getString(3));
map.put("Price", cursor.getString(4));
map.put("Currency", cursor.getString(5));
map.put("Desc", cursor.getString(6));
wordList.add(map);
} while (cursor.moveToNext());
}cursor.close();
database.close();
return wordList;
}
我调用数据库的活动如下:
items=db.retrieve_item_details(getcategory);
答案 0 :(得分:2)
我认为表格中缺少列名“类别”,请将表格交叉检查。
答案 1 :(得分:0)
在虚拟设备上运行应用程序,然后从数据/ data / yourapplication / database / yourdatabasename转到DDMS透视图,然后在您的计算机上拉出数据库并使用mozilla打开它。(注意:打开sqlite数据库在mozilla中你必须添加Sqlite manager add。) 现在您可以检查您的数据库。它可以帮助您轻松解决问题。
答案 2 :(得分:0)
Pasta
应该是'single quotes'
中的字符串文字,不能作为列名。
但是,最好在查询中使用参数。在SQL中用?
替换文字,并将bind参数中的值提供给查询函数:
String selectQuery = "SELECT * FROM item_table WHERE Category=?";
Cursor cursor = database.rawQuery(selectQuery, new String[] { category });