2015-09-12 147 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) 
);