有人有答案吗?
我的数据库助手类中的代码:
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
db.execSQL(CREATE_ITEM_DB);
}
public static final String ITEM_NAME = "item_name";
public static final String ITEM_STATUS = "item_status";
public static final String ITEM_DESCRIPTION = "item_description";
public static final String ITEM_CATEGORY = "item_category";
public static final String ITEM_ROW_ID = "row_id";
private static final String ITEM_TABLE = "table_of_items";
public static final String CREATE_ITEM_DB = "CREATE TABLE "
+ ITEM_TABLE + "(" + ITEM_ROW_ID +
" INTEGER PRIMARY KEY AUTOINCREMENT," + ITEM_NAME
+ " TEXT NOT NULL," + ITEM_STATUS + " TEXT NOT NULL," + ITEM_DESCRIPTION + " TEXT," + ITEM_CATEGORY + " TEXT NOT NULL," +
KEY_NAME + " TEXT);";
现在,在另一个类中,我调用:
public void list(){
myModel.open();
current_user = myModel.getCurUser();
Item [] items = (myModel.getLostItems(current_user));
if(items != null){
myView.setItem(items);
}
myModel.close();
}
myModel 是我的数据库类。它拥有我的数据库助手类和我的 sqlite 数据库的实例。此外, myModel.open() 获得一个可写数据库。
错误信息非常清楚:item_name 不是列。
但它看起来确实如此。
编辑:玩了之后,我决定这不是关于这个特定的列。似乎我放在列数组中的任何列都会导致相同的错误消息。
编辑 2:这是我的 getLostItems:
public Item[] getLostItems(String current_user) {
String [] columns = new String [] {DB_Helper.ITEM_NAME,
DB_Helper.ITEM_STATUS,
DB_Helper.ITEM_DESCRIPTION,
DB_Helper.ITEM_CATEGORY,
DB_Helper.ITEM_ROW_ID,
DB_Helper.KEY_NAME};
Cursor c = database.query(DB_Helper.DATABASE_TABLE_USERS, columns, DB_Helper.KEY_NAME + " = ? AND " + DB_Helper.ITEM_STATUS + " = ?",
new String[] {current_user, "lost"},
null,
null,
null);
从技术上讲,代码有点多,但是在使用调试器遍历代码后,它在查询语句上失败了。
日志:
03-02 16:44:56.375: E/SQLiteLog(16362): (1) no such column: item_name
03-02 16:44:56.375: D/AndroidRuntime(16362): Shutting down VM
03-02 06:47:46.825: E/AndroidRuntime(6760): FATAL EXCEPTION: main
03-02 06:47:46.825: E/AndroidRuntime(6760): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wheresmystuff/com.example.wheresmystuff.View.DisplayItems}: android.database.sqlite.SQLiteException: no such column: item_name (code 1): , while compiling: SELECT item_name, item_status, item_description, item_category, row_id, id_name FROM reg_users_table WHERE id_name = ? AND item_status = ?
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.os.Looper.loop(Looper.java:137)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 06:47:46.825: E/AndroidRuntime(6760): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 06:47:46.825: E/AndroidRuntime(6760): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 06:47:46.825: E/AndroidRuntime(6760): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 06:47:46.825: E/AndroidRuntime(6760): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 06:47:46.825: E/AndroidRuntime(6760): at dalvik.system.NativeStart.main(Native Method)
03-02 06:47:46.825: E/AndroidRuntime(6760): Caused by: android.database.sqlite.SQLiteException: no such column: item_name (code 1): , while compiling: SELECT item_name, item_status, item_description, item_category, row_id, id_name FROM reg_users_table WHERE id_name = ? AND item_status = ?
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
03-02 06:47:46.825: E/AndroidRuntime(6760): at com.example.wheresmystuff.Model.DB.getLostItems(DB.java:180)
03-02 06:47:46.825: E/AndroidRuntime(6760): at com.example.wheresmystuff.Presenter.ListingPresenter.list(ListingPresenter.java:33)
03-02 06:47:46.825: E/AndroidRuntime(6760): at com.example.wheresmystuff.Presenter.ListingPresenter.<init>(ListingPresenter.java:26)
03-02 06:47:46.825: E/AndroidRuntime(6760): at com.example.wheresmystuff.View.DisplayItems.onCreate(DisplayItems.java:26)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.app.Activity.performCreate(Activity.java:5104)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 06:47:46.825: E/AndroidRuntime(6760): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 06:47:46.825: E/AndroidRuntime(6760): ... 11 more