外键定义

时间:2012-03-31 16:11:37

标签: android sqlite

您好我正在尝试创建一个sqldatabase而我正在尝试在oncreate方法中定义我的外键。我只是想知道这是否是这样的正确定义:

路由表:id,name

停止表:id,name

时间表:id,总线的外键,停止的外键

    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_STOPTABLE + " (" +
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        KEY_STOPS + " TEXT NOT NULL);" +
        "CREATE TABLE " + DATABASE_BUSTABLE + " (" + 
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        KEY_BUSES + " TEXT NOT NULL);" +
        "CREATE TABLE " + DATABASE_TIMETABLE + " (" + 
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        KEY_BUSES + " TEXT NOT NULL, " +
                    + KEY_TIME + " integer,"
                    + " FOREIGN KEY ("+KEY_TIME+") REFERENCES "+DATABASE_STOPTABLE+"         ("+KEY_STOPS+"), " +
                      + " FOREIGN KEY ("+KEY_TIME+") REFERENCES "+DATABASE_BUSTABLE+"         ("+KEY_BUSES+"));"
                );
    }

1 个答案:

答案 0 :(得分:2)

没关系。请记住,您需要启用:

@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    if (!db.isReadOnly()) {
        db.execSQL("PRAGMA foreign_keys=ON;");
    }
}

'+'字符是KEY_TIME + " integer,"

之前的两倍