Sqlite异常:找不到列

时间:2011-11-29 17:30:24

标签: android sqlite exception

我构建了以下数据库:

   private static final String CREATE_TABLE_TEST=
        " create table " + TABLE_TEST +
        " (test_id integer primary key autoincrement," +
        COL_GRADE +" text not null," +
        DATE+" text not null);";



private static final String CREATE_TABLE_QUESTION=
        " create table " + TABLE_ANSWER +
        " (question_id integer primary key autoincrement," +
        COL_ANSWER_1+ " text not null,"+ 
        COL_ANSWER_2+" text not null," +
        COL_ANSWER_3+" text not null," +
        COL_ANSWER_4+" text not null," +
        COL_ANSWER_5+ " text not null,"+ 
        COL_ANSWER_6+" text not null," +
        COL_RIGHT_ANSWER+" integer," +
        COL_WRONG_ANSWER+" integer, " +
        COL_TEST_ID+" INTEGER, " + 
        "FOREIGN KEY(" + COL_TEST_ID + ") REFERENCES " + TABLE_TEST+"(test_id));";

到目前为止,错误仍然存​​在:

> 11-29 18:01:20.955: E/Database(853): Error inserting right_answer=6 wrong_answer=0 answer_5=fadsfas answer_6=gadsfdasasdfa
     

answer_3 = fasdfasf test_id = 1 answer_4 = fasdfsa answer_1 = fasdfasd   ANSWER_2 = fasdfas

11-29 18:01:20.955: E/Database(853): android.database.sqlite.SQLiteException: table answer has no column
     

命名 test_id :,编译时:INSERT INTO回答(right_answer,   wrong_answer,answer_5,answer_6,answer_3, test_id ,answer_4,   answer_1,answer_2)VALUES(?,?,?,?,?,?,?,?,?);

@Override
public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        Log.d("database", CREATE_TABLE_TEST); //I never see this statement in catlog
        Log.d("database", CREATE_TABLE_QUESTION); //nor this
        db.execSQL(CREATE_TABLE_QUESTION);
        db.execSQL(CREATE_TABLE_TEST);
    }

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_TEST);
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_ANSWER);
        onCreate(db);
    }

我把它放在oncreate ..:

 dbClass=new database(this);        
    try
    {
        Log.w("inside ",  " database opened");///This gets printed
        dbLite=dbClass.getWritableDatabase();
        Log.w("after ",  " database opened");///This get printed
    }
    catch(Exception c)
    {
        Log.v("Open database exception caught", c.getMessage());
        dbLite=dbClass.getReadableDatabase();
    }

onCreate中没有任何东西被打印出来..

好像onCreate永远不会被称为

4 个答案:

答案 0 :(得分:1)

您的外键未正确

http://www.sqlite.org/foreignkeys.html

了解更多信息

private static final String CREATE_TABLE_QUESTION=
" create table " + TABLE_ANSWER +
" (question_id integer primary key autoincrement," +
COL_ANSWER_1+ " text not null,"+ 
COL_ANSWER_2+" text not null," +
COL_ANSWER_3+" text not null," +
COL_ANSWER_4+" text not null," +
COL_ANSWER_5+ " text not null,"+ 
COL_ANSWER_6+" text not null," +
COL_RIGHT_ANSWER+" integer," +
COL_WRONG_ANSWER+" integer, " +
COL_TEST_ID+" INTEGER, " + 
"FOREIGN KEY(" + COL_TEST_ID + ") REFERENCES " + TABLE_TEST+"(test_id))";

应该做的伎俩

更进一步,您的onCreate方法仅在创建数据库时执行,而不是在升级时执行 你应该覆盖onUpgrade以执行更新(或在新设备/模拟器上测试)

http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onUpgrade(android.database.sqlite.SQLiteDatabase,%20int,%20int

如果扩展sqliteOpenHelper,则需要使用newVersionId调用super-constuctor来触发onUpgrade

public YourDatabase(Context context) {
    super(context, DatabaseName, null, DATABASE_VERSION);
}

提示:从手机/模拟器中完全卸载应用程序并重新安装(然后将删除数据库,如果不是问题出现在其他地方,则会立即创建数据库)

答案 1 :(得分:0)

盲目猜测:;之后你需要INTEGER REFERENCES test )吗?

答案 2 :(得分:0)

对我跳出的第一件事是在列名之后和create table指令中的列类型之前缺少空格...

COL_GRADE +“text not null,”+  DATE +“text not null);”;

您确定该表是否已正确创建?

答案 3 :(得分:0)

您可以尝试清除应用程序数据吗?有时数据库会在您拥有所有列之前创建,如果您的onUpgrade方法未正确实现,则在您清除数据之前,该列永远不会被添加。