在SQLite中使用引用

时间:2015-09-28 22:50:20

标签: java android sqlite

我正在尝试创建两个表(Users,Messages)。 users表,包含一个主键(RoomID),我将在Messages表中引用它。这是我试图运行的查询。

String CREATE_TABLE_MESSAGES = "CREATE TABLE " + Messages.TABLE  + "( "
                + "FOREIGN KEY(" + Messages.KEY_ROOMID  + ") REFERENCES "+ChatRooms.TABLE+"("+ChatRooms.KEY_ROOMID+"), "
                + Messages.MESSAGE+ " TEXT, "
                + Messages.SIDE+ " BOOLEAN, "
                + Messages.TYPE + " TEXT )";

但是,我一直遇到这个错误

09-28 18:45:14.803  14760-14789/lltest.mohsin.com.chattest E/SQLiteLog﹕ (1) near "FOREIGN": syntax error
09-28 18:45:14.807  14760-14789/lltest.mohsin.com.chattest W/System.err﹕ android.database.sqlite.SQLiteException: near "FOREIGN": syntax error (code 1): , while compiling: CREATE TABLE Messages( FOREIGN KEY(RoomID) REFERENCES ChatRooms(RoomID), Message TEXT, Side BOOLEAN, Type TEXT )
09-28 18:45:14.807  14760-14789/lltest.mohsin.com.chattest W/System.err﹕ at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-28 18:45:14.807  14760-14789/lltest.mohsin.com.chattest W/System.err﹕ at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
09-28 18:45:14.807  14760-14789/lltest.mohsin.com.chattest W/System.err﹕ at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
09-28 18:45:14.811  14760-14789/lltest.mohsin.com.chattest W/System.err﹕ at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的TwoNumbers gcdNumbers = GCD(numbers.getNum1(), numbers.getNum2()); display(gcdNumbers); TwoNumbers lcmNumbers = LCM(numbers.getNum1(), numbers.getNum2()); display(lcmNumbers); 定义应关注列定义。

请参阅SQLite手册中的示例:https://www.sqlite.org/foreignkeys.html

一旦这样做,它将再次失败,因为外键列(FOREIGN KEY)未定义。