「我遇到錯誤‘沒有這樣的表’,並導致我嘗試一切可能,使之消失,現在它走了,但我很迷惑,這裏是我的情況SQLite數據庫的Android創建表
我有2個我需要在應用程序中創建表,並且每個表放置一個類,它看起來像下面的代碼。如果我遵循這個方法,當我從「表2」中提取數據時,會觸發「沒有這樣的表」,所以我需要更改DATABASE_NAME到其他值=「DB2」;然後「Table2」將創建成功並能夠提取數據。
所以我的問題是這是一個正確的方法來確保2表創建成功?或者我不應該單獨的表分爲2類?請告知
public class DB1 extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DB";
// Labels table name
private static final String TABLE_STORE = "Table1";
// Labels Table Columns names
public static final String KEY_1 = "col1";
public RC_StoreTbl(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// Category table create query
String CREATE_TABLE = "CREATE TABLE " + TABLE_STORE + "("
+KEY_1 + " INTEGER PRIMARY KEY)";
db.execSQL(CREATE_TABLE);
}
}
另一類
public class DB2 extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DB";
// Labels table name
private static final String TABLE_STORE = "Table2";
// Labels Table Columns names
public static final String KEY_1 = "col1";
public RC_StoreTbl(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// Category table create query
String CREATE_TABLE = "CREATE TABLE " + TABLE_STORE + "("
+KEY_1 + " INTEGER PRIMARY KEY)";
db.execSQL(CREATE_TABLE);
}
}
對不起。但爲什麼你需要兩個單獨的類爲兩個表?這是不是也創造了兩個數據庫? – 2013-03-15 03:54:52
對不起,我想我被誤解了這個概念,我認爲它會把它當作同一個數據庫,並且創建不存在的表,我嘗試使它更簡潔,更容易被桌/類維護。 – tsohtan 2013-03-15 04:21:52
我想你應該給這個教程一個閱讀。這將爲你清理一些事情。 http://www.vogella.com/articles/AndroidSQLite/article.html – 2013-03-15 04:23:50