得到此异常:“android.database.sqlite.SQLiteException:没有这样的列:_id :,编译时”,当我有一个_id列

时间:2011-05-29 20:50:08

标签: java android sqlite

每当我运行我的应用程序时,我都会收到此异常。不知道是什么导致了它。以下是一些背景信息:

  • 我的全局属性中有这个:

    public static final String KEY_ID = "_id";
    
  • 接下来我有一个String变量来设置我的数据库列:

    private static final String DATABASE_CREATE =
                "create table sdCards (_id integer primary key autoincrement, "
                + "arg0 text not null, "
                + "arg1 integer not null, "
                        + "arg2 integer not null, "
                        + "arg3 integer not null, "
                        + "arg4 integer not null, "
                        + "arg5 integer not null);";
    
  • 最后,我按原样创建数据库:

    public void onCreate(SQLiteDatabase db) {
         db.execSQL(DATABASE_CREATE);
    }
    

不完全确定发生了什么。我显然有一个_id专栏!任何帮助将不胜感激!

编辑:这是我的logcat

E/AndroidRuntime(24036): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.packagename/com.mydomain.packagename.MainClass}: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, arg0, arg1, arg2, arg3, arg4, arg5 FROM table_name
E/AndroidRuntime(24036):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1741)
E/AndroidRuntime(24036):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1757)
E/AndroidRuntime(24036):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
E/AndroidRuntime(24036):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
E/AndroidRuntime(24036):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(24036):    at android.os.Looper.loop(Looper.java:126)
E/AndroidRuntime(24036):    at android.app.ActivityThread.main(ActivityThread.java:4002)
E/AndroidRuntime(24036):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(24036):    at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(24036):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
E/AndroidRuntime(24036):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
E/AndroidRuntime(24036):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(24036): Caused by: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, sdName, writes, modifications, moved, deletes FROM sdCards
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:147)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:368)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:131)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:95)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1534)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1414)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1370)
E/AndroidRuntime(24036):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1450)
E/AndroidRuntime(24036):    at com.mydomain.packagename.DbAdapter.fetchAllSd(DbAdapter.java:131)
E/AndroidRuntime(24036):    at com.mydomain.packagename.MainClass.fillData(SDBenchmark.java:70)
E/AndroidRuntime(24036):    at com.mydomain.packagename.MainClass.onCreate(SDBenchmark.java:28)
E/AndroidRuntime(24036):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
E/AndroidRuntime(24036):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1705)
E/AndroidRuntime(24036):    ... 11 more

1 个答案:

答案 0 :(得分:3)

尝试删除您的数据库(可能是卸载您的应用),然后尝试运行该应用。这将重新创建您的数据库。一种可能性是您已经创建了表,并且某个地方的选择失败,因为从未创建过_id列。