2012-03-18 88 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