在android中找不到列item_name

时间:2013-03-02 06:25:50

标签: android sqlite exception

有没有人有答案?

我的数据库助手类中的代码:

   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

2 个答案:

答案 0 :(得分:0)

你有一个额外的逗号而不是

KEY_NAME + " TEXT,

应该是

KEY_NAME + " TEXT);"

答案 1 :(得分:0)

这里,它在myModel.getLostItems()方法的select语句中显示错误消息,请检查您是否使用&#39;&#39;&#39;对于查询中的item_name列。