1

这是我创建数据库表的代码

String sqlQueryToCreateFileTable = "create table if not exists " + TABLE_FILE + " ( " + BaseColumns._ID + " integer primary key autoincrement, " 
+ COLUMN_NAME_FILE_NAME + " text not null, "
+ COLUMN_NAME_FILE_CATEGORY+ " integer not null , FOREIGN KEY ("+COLUMN_NAME_FILE_CATEGORY+") REFERENCES "+TABLE_CATEGORY+" ("+BaseColumns._ID+"), " 
+ COLLUMN_NAME_FILE_THEME+ " integer not null , FOREIGN KEY ("+COLLUMN_NAME_FILE_THEME+") REFERENCES "+TABLE_THEME+" ("+BaseColumns._ID+"), " 
+ COLLUMN_NAME_FILE_DATE_CREATING+ " integer not null , FOREIGN KEY ("+COLLUMN_NAME_FILE_DATE_CREATING +") REFERENCES "+TABLE_DATE+" ("+BaseColumns._ID+"), " 
+ COLLUMN_NAME_FILE_CLOUD + " text default null,"
+ COLLUMN_NAME_FILE_DATE_UPLOADING + " text default null);";

db.execSQL(sqlQueryToCreateFileTable);

但我在logcat中有那个

06-15 18:24:09.177: I/Process(9075): Sending signal. PID: 9075 SIG: 9
06-15 18:31:01.428: I/Database(10559): sqlite returned: error code = 1, msg = near "file_theme_column": syntax error
06-15 18:31:01.428: E/Database(10559): Failure 1 (near "file_theme_column": syntax error) on 0x5882f8 when preparing 'create table if not exists file_table ( _id integer primary key autoincrement, file_name_column text not null, file_category_column integer not null , FOREIGN KEY (file_category_column) REFERENCES category_table (_id), file_theme_column integer not null , FOREIGN KEY (file_theme_column) REFERENCES theme_table (_id), file_date_creating_column integer not null , FOREIGN KEY (file_date_creating_column) REFERENCES date_table (_id), file_cloud_column text default null,file_date_upload_column text default null);'.
06-15 18:31:01.468: D/AndroidRuntime(10559): Shutting down VM
06-15 18:31:01.468: W/dalvikvm(10559): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-15 18:31:01.488: E/AndroidRuntime(10559): FATAL EXCEPTION: main
06-15 18:31:01.488: E/AndroidRuntime(10559): android.database.sqlite.SQLiteException: near "file_theme_column": syntax error: create table if not exists file_table ( _id integer primary key autoincrement, file_name_column text not null, file_category_column integer not null , FOREIGN KEY (file_category_column) REFERENCES category_table (_id), file_theme_column integer not null , FOREIGN KEY (file_theme_column) REFERENCES theme_table (_id), file_date_creating_column integer not null , FOREIGN KEY (file_date_creating_column) REFERENCES date_table (_id), file_cloud_column text default null,file_date_upload_column text default null);
06-15 18:31:01.488: E/AndroidRuntime(10559):    at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
06-15 18:31:01.488: E/AndroidRuntime(10559):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
06-15 18:31:01.488: E/AndroidRuntime(10559):    at com.app.pfe.AndroidOpenDbHelper.onCreate(AndroidOpenDbHelper.java:95)
06-15 18:31:01.488: E/AndroidRuntime(10559):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
 how can I fix this error?
4

2 回答 2

4

表约束应该到最后:

http://www.sqlite.org/lang_createtable.html

于 2012-06-15T18:50:19.247 回答
2

就像 pawelzieba 说的,你应该尝试重新格式化

create table if not exists file_table ( 
    _id integer primary key autoincrement, 
    file_name_column text not null, 
    file_category_column integer not null, 
    file_theme_column integer not null, 
    file_date_creating_column integer not null, 
    file_cloud_column text default null,
    file_date_upload_column text default null,
    FOREIGN KEY (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)
);
于 2012-06-15T18:59:40.540 回答