[英]SQLlite db creating table error
您好,我正在使用SQLite在Android中進行項目。 我正在嘗試創建一個數據庫(目前),該數據庫具有三個表:(TABLE_DR_TYPE,TABLE_DR_TITLE,TABLE_USERS)創建表USERS時,我在logcat中始終遇到此錯誤。 有什么幫助嗎?
//TABLE DR TYPE
private static final String TABLE_DR_TYPE = "table_doctor_type";
private static final String DR_TYPE_COLUMN_ID = "id";
private static final String DR_TYPE_COLUMN_DR_TYPE = "dr_type";
// TABLE DOCTOR_TITLE
private static final String TABLE_DR_TITLE = "table_doctor_title";
private static final String DR_TITLE_COLUMN_ID = "id";
private static final String DR_TITLE_COLUMN_DR_TITLE = "dr_title";
// TABLE USERS
private static final String TABLE_USERS = "users";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_SURNAME = "surname";
private static final String COLUMN_TITLE = "title_id"; // FK
private static final String COLUMN_DR_TYPE= "type_id"; // FK
private static final String COLUMN_PIN= "pin";
private static final String COLUMN_EMAIL= "email";
private static final String COLUMN_TEL= "telephone";
//CREATE TABLEs
private static final String CREATE_TABLE_DR_TYPE = "CREATE TABLE "
+ TABLE_DR_TYPE + "("
+ DR_TYPE_COLUMN_ID + " integer primary key, "
+ DR_TYPE_COLUMN_DR_TYPE + " text not null);";
private static final String CREATE_TABLE_DR_TITLE= "CREATE TABLE "
+ TABLE_DR_TITLE + "("
+ DR_TITLE_COLUMN_ID + " integer primary key, "
+ DR_TITLE_COLUMN_DR_TITLE + " text not null);";
private static final String CREATE_TABLE_USER = "CREATE TABLE "
+ TABLE_USERS + " ("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_NAME + " text not null, "
+ COLUMN_SURNAME + " text not null, "
+ COLUMN_TITLE + " integer, "
+ " FOREIGN KEY ("+COLUMN_TITLE+") REFERENCES "+TABLE_DR_TITLE+"("+DR_TITLE_COLUMN_ID+"), "
+ COLUMN_DR_TYPE + " integer, "
+ " FOREIGN KEY ("+COLUMN_DR_TYPE+") REFERENCES "+TABLE_DR_TYPE+"("+DR_TYPE_COLUMN_ID+"), "
+ COLUMN_PIN + " text not null, "
+ COLUMN_EMAIL + " text not null, "
+ COLUMN_TEL + " text not null);";
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("Kreiram bazu");
db.execSQL("PRAGMA foreign_keys=ON");
db.execSQL(CREATE_TABLE_DR_TYPE);
db.execSQL(CREATE_TABLE_DR_TITLE);
db.execSQL(CREATE_TABLE_USER);
this.db = db;
}
日志貓:
Caused by: android.database.sqlite.SQLiteException: near "type_id": syntax error (code 1): , while compiling: CREATE TABLE users (id integer primary key autoincrement, name text not null, surname text not null, title_id integer, FOREIGN KEY (title_id) REFERENCES table_doctor_title(id), type_id integer, FOREIGN KEY (type_id) REFERENCES table_doctor_type(id), pin text not null, email text not null, telephone text not null);
根據該圖 ,外鍵位於字段定義之后。
因此,用戶表應如下所示:
private static final String CREATE_TABLE_USER = "CREATE TABLE "
+ TABLE_USERS + " ("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_NAME + " text not null, "
+ COLUMN_SURNAME + " text not null, "
+ COLUMN_TITLE + " integer, "
+ COLUMN_DR_TYPE + " integer, "
+ COLUMN_PIN + " text not null, "
+ COLUMN_EMAIL + " text not null, "
+ COLUMN_TEL + " text not null, "
+ " FOREIGN KEY ("+COLUMN_TITLE+") REFERENCES "+TABLE_DR_TITLE+"("+DR_TITLE_COLUMN_ID+"), "
+ " FOREIGN KEY ("+COLUMN_DR_TYPE+") REFERENCES "+TABLE_DR_TYPE+"("+DR_TYPE_COLUMN_ID+") "
+");";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.