簡體   English   中英

無法插入sqlite數據庫,錯誤:表不存在

[英]can't insert into sqlite database, error: Table doen't exist

我一直在嘗試解決過去三天似乎很簡單的問題,我正在嘗試從活動中將記錄插入到sqlite數據庫中,而我一直在尋找該問題而沒有外觀。 在下面找到我的活動代碼和DBHelper代碼。 dbhelper代碼:

  public Long insertResult(String QUEST_ID,String test1,String test2) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues initialValues = new ContentValues();
    initialValues.put("Question", QUEST_ID);
    initialValues.put("Rights", test1);
    initialValues.put("Wrongs", test2);
    Log.d("Result:", "Rights " + initialValues.get("Rights"));
    return  db.insertOrThrow("Results", null, initialValues);
}

from the mainactivity
 long ins = myDbHelper.insertResult("test","test2","test3");

結果表在數據庫中不存在。

看起來您的DBHelper正在擴展SQLiteOpenHelper 您應該在DBHelper重寫onCreate方法。 例如,添加如下內容:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table Results (_id integer primary key autoincrement, Question text not null, Rights text not null, Wrongs text not null);");
}

如果您曾經更改過數據庫架構,則還需要重寫onUpgrade方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM