我在Route_has_Stops表中遇到错误
private static final String CREATE_TABLE_ROUTE_HAS_STOPS="CREATE TABLE " + TABLE_ROUTE_HAS_STOPS + "("
+ KEY_ROUTE_HAS_STOP_ID + " INTEGER PRIMARY KEY,"
+ KEY_ROUTE_ID + " INTEGER,"
+ "FOREIGN KEY ("+KEY_ROUTE_ID+") REFERENCES "+TABLE_Route+" ("+KEY_ROUTE_ID+")"
+ KEY_STOP_ID + " INTEGER,"
+ "FOREIGN KEY ("+KEY_STOP_ID+") REFERENCES "+TABLE_Stops+" ("+KEY_STOP_ID+"))";
错误
android.database.sqlite.SQLiteException: near "Stop_id": syntax error (code 1): , while compiling: CREATE TABLE Route_has_Stops(ID INTEGER PRIMARY KEY,Route_id INTEGER,FOREIGN KEY (Route_id) REFERENCES Route (Route_id)Stop_id INTEGER,FOREIGN KEY (Stop_id) REFERENCES Stops (Stop_id))
答案 0 :(得分:2)
您不能混合表约束,例如FOREIGN KEY
和列规范。首先指定列,并将FOREIGN KEY
表约束放在CREATE TABLE
。
答案 1 :(得分:1)
您在stop_id
之前缺少逗号:
private static final String CREATE_TABLE_ROUTE_HAS_STOPS="CREATE TABLE " + TABLE_ROUTE_HAS_STOPS + "("
+ KEY_ROUTE_HAS_STOP_ID + " INTEGER PRIMARY KEY,"
+ KEY_ROUTE_ID + " INTEGER,"
+ "FOREIGN KEY ("+KEY_ROUTE_ID + ") REFERENCES "
+ TABLE_Route+" ("+KEY_ROUTE_ID+")," // This comma was missing
+ KEY_STOP_ID + " INTEGER,"
+ "FOREIGN KEY ("+KEY_STOP_ID+") REFERENCES "+TABLE_Stops+" ("
+ KEY_STOP_ID+"))";
答案 2 :(得分:0)
我首先声明了Stop和Route键约束然后声明了它们的外键后解决了我的问题..
private static final String CREATE_TABLE_ROUTE_AS_STOP="CREATE TABLE "+TABLE_ROUTE_AS_STOP+"("
+ KEY_ROUTE_HAS_STOP_ID + " INTEGER PRIMARY KEY,"
+ KEY_ROUTE_ID + " INTEGER," + KEY_STOP_ID + " INTEGER,"
+ "FOREIGN KEY ("+KEY_ROUTE_ID+") REFERENCES "+TABLE_Route+" ("+KEY_ROUTE_ID+"),"
+ "FOREIGN KEY ("+KEY_STOP_ID+") REFERENCES "+TABLE_Stops+" ("+KEY_STOP_ID+"))";
答案 3 :(得分:-1)
这样我就可以创建我的Sqlite数据库表,工作正常
DatabaseHelper.java
package com.example.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DataBaseHelper_forsignUp extends SQLiteOpenHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "firstname";
public static final String KEY_LNAME = "lastname";
public static final String KEY_EMAIL = "email";
public static final String KEY_PHONENUMBER = "Phonenumber";
public static final String KEY_PASSWORD = "Password";
public static final String KEY_CONFPASSWORD = "ConfPassword";
public static final String KEY_UPDATESTATUS = "Status";
public static final String KEY_DATEOFBIRTH="Dateofbirth";
public static final String KEY_LOCATION="Location";
public static final String KEY_ADDRESS="Address";
private static String DATABASE_NAME = "Signup";
public static String TABLE_NAME = "SIGNUP";
public DataBaseHelper_forsignUp(Context context, String name,
CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createQuery = "CREATE TABLE " + TABLE_NAME + "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ " firstname TEXT NOT NULL,"
+ " lastname TEXT NOT NULL, "
+ " email TEXT NOT NULL,"
+ " Phonenumber TEXT NOT NULL,"
+ " Password TEXT NOT NULL,"
+ " Dateofbirth TEXT ,"
// + " Gender TEXT ,"
+ " Location TEXT ,"
+ " Address TEXT ,"
+ " Status TEXT NOT NULL);";
db.execSQL(createQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
}