Android Sqlite异常:未找到列

Android Sqlite异常:未找到列,android,sqlite,exception,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 fi

我构建了以下数据库:

   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));";
到目前为止,错误仍然存在:

答案3=fasdfasf测试id=1答案4=fasdfsa答案1=fasdfasd 答案2=fasdfas

11-29 18:01:20.955: E/Database(853): android.database.sqlite.SQLiteException: table answer has no column
命名为测试id:,编译时:插入到答案中(右答案, 回答错误,回答5,回答6,回答3,测试id,回答4, 答案1,答案2)值(?,,,,,,,,,,,,,,,?)

我把它放在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从未被调用过一样

盲目猜测:你需要一个
整数引用测试之后)

我首先想到的是在create table指令中,列名之后和列类型之前缺少空格

COL_GRADE+“文本不为空,”+ 日期+“文本不为空);”


是否确实正确创建了表?

能否尝试清除应用程序数据?有时,在数据库中包含所有列之前就创建了数据库,如果onUpgrade方法没有正确实现,则在清除数据之前,将无法添加该列。

外键没有正确生成

更多信息

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以执行更新(或在新设备/仿真器上测试)

)

如果扩展sqliteOpenHelper,则需要使用newVersionId调用超级构造函数来触发onUpgrade

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

提示:从手机/仿真器中完全卸载应用程序,然后重新安装(然后将删除db并最终创建它,如果不是,问题在其他地方)

您能告诉我们您的insert语句吗?此外,在References test子句上,您是否需要指定表和列(即test.test\u id)?在创建表之前,执行此操作并回发结果日志.d(“数据库”,CREATE\u table\u test);Log.d(“数据库”,创建表);我没有看到报表打印出来。。。。。在异常发生11-29 17:47:19.505:I/数据库(671):sqlite返回:错误代码=1,msg=表答案没有名为test_idi的列如果我第二次单击,则数据库未打开..:(可能是个愚蠢的问题,但是你的代码每次都在创建一个没有表的空数据库吗?你每次运行应用程序时都在代码中创建这两个表吗?在插入之前,我会检查你的答案。。等一分钟。不,它仍然说我缺少一个测试。\u idyeah,我用它来查询它的最后一个IDF。出于某种原因,它没有。。我认为我创建的数据库类中的onCreate方法没有发生…请在我的问题末尾查看我的问题更新…等等,对不起..我确实认为你是对的..让我再次检查你的答案,它仍然说它没有列测试id..可能数据库打开的方式有问题吗?!?它确实插入了测试id..但它仍然显示消息是否更改了您的新版本int?否则onUpgrade将不会触发公共数据库(上下文){super(上下文,DB_名称,null,DB_版本);//TODO自动生成的构造函数存根}当我调用getWritableDatabase时,onCreate没有被调用的问题…我的意思是,,,没有引发异常..方法被调用,,,但onCreate方法中的任何内容都不会被提交给logcat
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))";
public YourDatabase(Context context) {
    super(context, DatabaseName, null, DATABASE_VERSION);
}