2012-06-15 48 views
0

我要創建四個表女巫一個(nammed table_file)數據庫,數據庫中有來自其他表的外鍵,第一個我嘗試創建在同一時間的所有表,但table_file給我相關的外國錯誤鍵,所以我評論在我的DBhelper中創建這個表的代碼,並且我成功創建了其他表,但是當我取消註釋創建代碼的tabe_file時,試着創建這個表,它說:sqlite返回:error code = 1,msg =沒有這樣的表:file_table和我,在logcat的創建於Android

06-15 13:26:19.841: E/Database(24784): Error inserting file_category_column=0 file_theme_column=0 file_name_column=priv_priv_222_2012-06-15.pdf file_date_creating_column=1 
06-15 13:26:19.841: E/Database(24784): android.database.sqlite.SQLiteException: no such table: file_table: , while compiling: INSERT INTO file_table(file_category_column, file_theme_column, file_name_column, file_date_creating_column) VALUES(?, ?, ?, ?); 
06-15 13:26:19.841: E/Database(24784): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
06-15 13:26:19.841: E/Database(24784): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 
06-15 13:26:19.841: E/Database(24784): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65) 
06-15 13:26:19.841: E/Database(24784): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83) 
06-15 13:26:19.841: E/Database(24784): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41) 
06-15 13:26:19.841: E/Database(24784): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149) 
06-15 13:26:19.841: E/Database(24784): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569) 
06-15 13:26:19.841: E/Database(24784): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426) 

我該怎麼辦創建該表?

+0

你需要向我們展示你的代碼,而不僅僅是錯誤。 – Barak

+0

巫代碼,你需要我的dbhelper類或其它類,我在表 – user1381760

+0

插入我問乳清它沒有加我的文件表,這可能與Android源碼在任何我們喜歡的時間將表添加到現有的數據庫? – user1381760

回答

0

請參閱SQLite上的本教程。

http://www.vogella.com/articles/AndroidSQLite/article.html

如果它在抱怨沒有這樣的表,那麼該表不存在。如果您要升級當前模式,請確保您正在更新數據庫版本,以便調用onUpgrade(),並確保您正在onUpgrade()中工作,以便重新創建表。

+0

我的問題不是如何創建數據庫,但如何創建與外鍵的表,當我嘗試我有android.database.sqlite.SQLiteException:外鍵定義中的未知列「file_category_column」:創建表如果不存在file_table(_id整數(file_category_column)REFERENCES category_table(_id),FOREIGN KEY(file_theme_column)REFERENCES theme_table(_id),FOREIGN KEY(file_date_creating_column)REFERENCES date_table(_id),file_cloud_column text默認爲null,file_date_upload_column text默認值爲null,file_date_upload_column爲文本默認值空值); – user1381760

+0

夠公平的,但是你的錯誤信息清楚地告訴你,你所引用的表不存在......所以我只能假設你失敗了,因爲表不在那裏。一旦表格確實存在,您可能會收到另一個錯誤,但直到它出現爲止,您無法與之建立關係。由於您創建表的代碼沒有列出,我唯一需要做的事情就是如何確保實際創建所需的表。 – stuckless

0

我用一個類的一些功能,您可以嘗試:

public PLSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version, String tableNames[], String fieldNames[][], String fieldTypes[][]) { 
    super(context, name, factory, version); 
    TableNames = tableNames; 
    FieldNames = fieldNames; 
    FieldTypes = fieldTypes; 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    if(TableNames == null){ 
     message = NO_CREATE_TABLES; 
     return; 
    } 

    for(int i = 0; i < TableNames.length; i++){ 
     String sql = "CREATE TABLE " + TableNames[i] + "("; 
     for(int j = 0; j < FieldNames[i].length; j++) 
      sql += FieldNames[i][j] + " " + FieldTypes[i][j] + ","; 
     sql = sql.substring(0, sql.length() - 1); 
     sql += ")"; 
     db.execSQL(sql); 
    } 
} 

以上只是創建功能。你可以試着看看它。

0

到另一個表添加到現有的數據庫,(假設你在首位的應用程序中創建它),你可以改變你的onUpgrade方法。

onUpgrade被稱爲每當數據庫需要升級。它確定通過檢查您提供的版本號與上次打開數據庫時存儲的版本號。您必須遞增VERSION_NUMBER才能調用onUpgrade

@Override 
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.executeSQL(DATABASE_CREATETABLE2); 
} 

DATABASE_CREATETABLE2將持有的SQL語句來創建表(大概就像你做你的其他表)的字符串。