由于 FOREIGN KEY 语法错误,我的表没有被创建,但我似乎无法弄清楚。在调用 FK 之前,我确保首先创建了 int。这是我的 FK 课程的代码
public class PlayerStatsDatabase {
public static final String KEY_ROWID = "_id";
public static final String PLAYER_ID = "PlayerId";
public static final String KEY_SCORE = "Goals_Scored";
public static final String KEY_MINUTES = "Minutes_Played";
public static final String KEY_SUBIN = "Substitute_In";
public static final String KEY_SUBOUT = "Substitute_Out";
public static final String KEY_BOOKING = "Booked";
private static final String TAG = "PlayersStatsDatabase";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "Players";
private static final String SQLITE_TABLE = "PlayerStats";
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement," +
PLAYER_ID + "integer"+" FOREIGN KEY ("+PLAYER_ID+") REFERENCES "+DATABASE_NAME+" ("+KEY_ROWID+"),"+
KEY_SCORE + "," +
KEY_MINUTES + "," +
KEY_SUBIN + "," +
KEY_SUBOUT + "," +
KEY_BOOKING + ")" ;
主键在这个类中
public class PlayerDbAdapter {
private static final String DATABASE_TABLE = "Players";
private static final String DATABASE_CREATE = "CREATE TABLE "+DATABASE_TABLE+" (_id integer primary key autoincrement, Player_Name text not null, Player_Position text not null, Player_Number text not null, Team text not null);";
private static final int DATABASE_VERSION =1;
public static final String KEY_BODY = "Player_Name";
public static final String KEY_ROWID = "_id";
public static final String KEY_TITLE = "Player_Position";
public static final String KEY_NUMBER = "Player_Number";
public static final String KEY_TEAM = "Team";
private static final String TAG = "PlayerDbAdapter";
private final Context mCtx;
private SQLiteDatabase mDb;
private DatabaseHelper mDbHelper;
public PlayerDbAdapter(Context paramContext)
{
this.mCtx = paramContext;
}
public void close()
{
this.mDbHelper.close();
}
public long createPlayers(String playerName, String playerPosition, String playerNumber, String team)
{
ContentValues localContentValues = new ContentValues();
localContentValues.put(KEY_BODY, playerName);
localContentValues.put(KEY_TITLE, playerPosition);
localContentValues.put(KEY_NUMBER, playerNumber);
localContentValues.put(KEY_TEAM, team);
try{
return this.mDb.insert("Players", null, localContentValues);
} catch (Exception e) {
Log.e("Juma", e.getMessage());
}
return 0;
}
日志猫
04-18 19:28:08.544: E/Database(352): Failure 1 (near "FOREIGN": syntax error) on 0x2b5840 when preparing 'CREATE TABLE if not exists PlayerStats (_id integer PRIMARY KEY autoincrement,PlayerIdinteger FOREIGN KEY (PlayerId) REFERENCES Players (_id),Goals_Scored,Minutes_Played,Substitute_In,Substitute_Out,Booked)'.
是因为我没有在 oNCreate 方法上插入 Player_id 吗?