2012-03-18 94 views
1

我在Android应用程序中遇到了数据库问题。下面是一些代码:Android SQLite异常没有这样的列

private static final String QUESTIONS_TABLE = "questions"; 
/* questions keys */ 
public static final String KEY_ROWID = "_id"; 
public static final String KEY_TYPE = "type"; 
public static final String KEY_CONTENT = "content"; 
public static final String KEY_A = "answerA"; 
public static final String KEY_B = "answerB"; 
public static final String KEY_C = "answerC"; 
public static final String KEY_D = "answerD"; 

private static final String QUESTIONS_TABLE_CREATE = 
    "create table "+ QUESTIONS_TABLE +" (" + KEY_ROWID + " integer primary key autoincrement, " 
    + KEY_TYPE + "integer not null," 
    + KEY_CONTENT + " text not null, " 
    + KEY_A + " text, " 
    + KEY_B + "text, " 
    + KEY_C + "text, " 
    + KEY_D + "text); "; 

//--- QUESTIONS SECTION --- 
//--- inserts a close question --- 
public long insertCloseQuestion(int type, String content, String[] answers) { 
    ContentValues initValues = new ContentValues(); 
    initValues.put(KEY_TYPE, type); 
    initValues.put(KEY_CONTENT, content); 
    if(answers != null && answers.length > 0) { 
     initValues.put(KEY_A, answers[0]); 
     initValues.put(KEY_B, answers[1]); 
     initValues.put(KEY_C, answers[2]); 
     initValues.put(KEY_D, answers[3]); 
    } 
    Log.d("VM", initValues.toString()); 

    return db.insertOrThrow(QUESTIONS_TABLE, null, initValues); 
} 

当我打电话insertCloseQuestion方法,安卓返回一个例外:

android.database.sqlite.SQLiteException: table questions has no column named answerD: , while compiling: INSERT INTO questions(answerD, content, answerB, answerC, type, answerA) VALUES(?, ?, ?, ?, ?, ?); 

你有什么想法如何解决呢?我知道这里有几个主题,但没有任何解决方案对我有帮助。

回答

4
private static final String QUESTIONS_TABLE_CREATE = 
    "create table "+ QUESTIONS_TABLE +" (" + KEY_ROWID + " integer primary key autoincrement, " 
    + KEY_TYPE + "integer not null," 
    + KEY_CONTENT + " text not null, " 
    + KEY_A + " text, " 
    + KEY_B + "text, " 
    + KEY_C + "text, " 
    + KEY_D + "text); "; 

我想列名和它的类型之间没有空格。 KEY_B + "text, "在文本之前放置一个空格(这里和其他列)。

然后不要忘记删除数据库(从手机中卸载应用程序)!在你的代码

+0

那是时机:) – KarlKarlsom 2012-03-18 14:16:07

+0

它的工作原理!非常感谢:)(我还错过了KEY_TYPE之后的空间)。当我读到你的答案时,这很明显。但是,当我在30分钟后出演这段代码时,我找不到任何错误:D – Kraxi 2012-03-18 14:19:32

2

错误之一是在String QUESTIONS_TABLE_CREATE 您的代码被发现使用+ KEY_B +“的文字,”这将导致最终的字符串中answerBtext。 在文本的开头添加一个空格。 下面的代码应该修复它。

如果您的代码中没有其他错误(您在这里显示的部分对我来说似乎没问题)。这应该可以解决你的问题:

private static final String QUESTIONS_TABLE_CREATE = 
    "create table "+ QUESTIONS_TABLE +" (" + KEY_ROWID + " integer primary key autoincrement, " 
    + KEY_TYPE + " integer not null," 
    + KEY_CONTENT + " text not null, " 
    + KEY_A + " text, " 
    + KEY_B + " text, " 
    + KEY_C + " text, " 
    + KEY_D + " text); "; 
+0

谢谢,@Yury也发现了这个错误:) – Kraxi 2012-03-18 14:20:08

+0

这里也是我的一个功劳)我估计应该与时间有关。但在目前情况下,如果出现1分钟的差异,我认为这是不可能的。 – Yury 2012-03-18 14:32:38