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)
声明有什么问题?
答案 0 :(得分:2)
您在create table语句中有一个多余的结束语:
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)
);