2017-11-27 10 views
0

こんにちは、私はAndroidのプロジェクトでSQLiteを使って作業しています。 (現時点で)3つのテーブルを持つデータベースを作成しようとしています。(TABLE_DR_TYPE、TABLE_DR_TITLE、TABLE_USERS)USERSテーブルを作成するときにlogcatでこのエラーが発生し続けます。助けてください?SQLlite dbテーブル作成エラー

//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; 
} 

logcat:

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); 

答えて

0

diagramによると、外部キーは、フィールド定義の後になります。

だから、usersテーブルは次のようにする必要があります:クエリでAUTOINCREMENTを使用することが正しいことか、自動インクリメントの場合

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+") " 
     +");"; 
+0

はただ不思議 –

関連する問題