2015-09-12 3 views
0
private static final String MINDMAPS_TABLE_CREATE = 
     "create table mindmaps (_id integer primary key autoincrement, " 
       + "title text not null);"; 

    private static final String BUBBLES_TABLE_CREATE = 
    "create table bubbles (_id integer primary key autoincrement, " 
    + "title text not null), " + 
      "_bmid integer, " + 
      " FOREIGN KEY (_bmid) REFERENCES mindmaps (_id))"; 


private static class DatabaseHelper extends SQLiteOpenHelper { 

    DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

     db.execSQL(MINDMAPS_TABLE_CREATE); 
     db.execSQL(BUBBLES_TABLE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
       + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS bubbles"); 
     db.execSQL("DROP TABLE IF EXISTS mindmaps"); 
     onCreate(db); 
    } 
} 

Я получить исключение следующим образом:Невозможно создать таблицу с внешним ключом

Caused by: android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: create table bubbles (_id integer primary key autoincrement, title text not null), _bmid integer, FOREIGN KEY (_bmid) REFERENCES mindmaps (_id)) 
      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 

Что не так с утверждением?

ответ

2

у вас есть лишняя закрывающая скобка в вашем заявлении создания таблицы:

create table bubbles (_id integer primary key autoincrement, 
    title text not null), 
-----------------------^ 
    bmid integer, 
    FOREIGN KEY (_bmid) REFERENCES mindmaps (_id)) 

Если вы удалите его, код должен работать.

Кроме того, auto_increment имеет знак подчеркивания.

Here - это SQL-скрипт с рабочим кодом.

create table mindmaps (
    _id integer primary key auto_increment, 
    title text not null 
); 

create table bubbles (_id integer primary key auto_increment, 
    title text not null, 
    _bmid integer, 
    FOREIGN KEY (_bmid) REFERENCES mindmaps (_id) 
); 
Смежные вопросы