需要Android SQLite外键帮助吗?

时间:2011-09-29 07:01:50

标签: android sqlite

// HANDLER

private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_CONTENT1 + " text not null);";

private static final String SCRIPT_CREATE_DATABASE2 = "create table " + MYDATABASE_TABLE2 + " (" + KEY_ID2 + " integer foreign key autoincrement, " + KEY_CONTENT2 + " text not null, " + KEY_CONTENT3 + " text not null);";

任何想法我可能做错了什么?

// ERROR LOG

> 09-29 11:54:40.052: INFO/Database(484): sqlite returned: error
     

code =

     
    

1,msg = near“foreign”:语法错误09-29 11:54:40.052:     错误/数据库(484):失败1(接近“外来”:语法错误)打开     0x217ba8准备'创建表MY_TABLE2(_id2整数     国外     key autoincrement,Content2 text not null,Content3 text not not     空值);'。     09-29 11:54:40.062:DEBUG / AndroidRuntime(484):关闭VM     09-29     11:54:40.062:WARN / dalvikvm(484):threadid = 1:线程退出     同     未捕获的异常(组= 0x4001d800)09-29 11:54:40.082:     错误/ AndroidRuntime(484):致命异常:主要09-29     11:54:40.082:     ERROR / AndroidRuntime(484):java.lang.RuntimeException:无法执行     开始     活动ComponentInfo {sep.com/sep.com.SepActivity}:     android.database.sqlite.SQLiteException:靠近“foreign”:语法     错误:     创建表MY_TABLE2(_id2整数外键自动增量,     Content2文本不为null,Content3文本不为null); 09-29     11:54:40.082:     ERROR / AndroidRuntime(484):at

         

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)     09-29 11:54:40.082:ERROR / AndroidRuntime(484):at

         

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)     09-29 11:54:40.082:ERROR / AndroidRuntime(484):at     android.app.ActivityThread.access $ 2300(ActivityThread.java:125)     09-29     11:54:40.082:ERROR / AndroidRuntime(484):at

         

android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)     09-29 11:54:40.082:ERROR / AndroidRuntime(484):at     android.os.Handler.dispatchMessage(Handler.java:99)09-29     11:54:40.082:ERROR / AndroidRuntime(484):at     android.os.Looper.loop(Looper.java:123)09-29 11:54:40.082:     错误/ AndroidRuntime(484):at     android.app.ActivityThread.main(ActivityThread.java:4627)09-29     11:54:40.082:ERROR / AndroidRuntime(484):at     java.lang.reflect.Method.invokeNative(Native Method)09-29     11:54:40.082:ERROR / AndroidRuntime(484):at     java.lang.reflect.Method.invoke(Method.java:521)09-29     11:54:40.082:     ERROR / AndroidRuntime(484):at

         

com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)     09-29 11:54:40.082:ERROR / AndroidRuntime(484):at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)     09-29     11:54:40.082:ERROR / AndroidRuntime(484):at     dalvik.system.NativeStart.main(Native Method)09-29     11:54:40.082:     ERROR / AndroidRuntime(484):引起:     android.database.sqlite.SQLiteException:靠近“foreign”:语法     错误:     创建表MY_TABLE2(_id2整数外键自动增量,     Content2文本不为null,Content3文本不为null); 09-29     11:54:40.082:     错误/ AndroidRuntime(484):at     android.database.sqlite.SQLiteDatabase.native_execSQL(母语     方法)     09-29 11:54:40.082:ERROR / AndroidRuntime(484):at

         

android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)     09-29 11:54:40.082:ERROR / AndroidRuntime(484):at     sep.com.handle $ SQLiteHelper.onCreate(handle.java:104)09-29     11:54:40.082:ERROR / AndroidRuntime(484):at

         

android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)     09-29 11:54:40.082:ERROR / AndroidRuntime(484):at     sep.com.handle.openToWrite(handle.java:44)09-29 11:54:40.082:     错误/ AndroidRuntime(484):at     sep.com.SepActivity.onCreate(SepActivity.java:38)09-29     11:54:40.082:     ERROR / AndroidRuntime(484):at

         

android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)     09-29 11:54:40.082:ERROR / AndroidRuntime(484):at

         

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)     09-29 11:54:40.082:ERROR / AndroidRuntime(484):... 11更多

  

2 个答案:

答案 0 :(得分:0)

// Creating tables
private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_CONTENT1 + " text not null);";

private static final String SCRIPT_CREATE_DATABASE2 = "create table " + MYDATABASE_TABLE2 + " (" + KEY_ID2 + " integer autoincrement, " + KEY_CONTENT2 + " text not null, " + KEY_CONTENT3 + " text not null, FOREIGN KEY ("+KEY_ID2+") REFERENCES "+PARENT_TABLE+" ("+PARENT_TABLE_PRIMARY+"));";

外键有引用

答案 1 :(得分:0)

试试这个......             字符串CREATE_MATCH =“如果不存在则创建表”+ TABLE_MATCH                     +“(”                         + MatchID +“INTEGER PRIMARY KEY AUTOINCREMENT”,                         + MatchTeamA +“INTEGER REFERENCES”+ TABLE_TEAM +“,”
                        + MatchTeamB +“INTEGER REFERENCES”+ TABLE_TEAM +“)”;