因为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;
}
logcat的
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吗?
答案 0 :(得分:1)
这是我从SQLite documentation获取的一个修改示例:
CREATE TABLE track(
KEY_ROWID INTEGER PRIMARY KEY autoincrement,
PLAYER_ID INTEGER,
KEY_SCORE TEXT,
FOREIGN KEY(PLAYER_ID) REFERENCES anotherTable(THE_KEY_ROWID_HERE),
FOREIGN KEY(KEY_ROWID, PLAYER_ID)
REFERENCES yetAnotherTable(THE_KEY_ROWID_THERE, THE_PLAYER_ID_THERE)
);
如您所见,您必须在所有列定义之后添加外键 - 不要尝试将放在列定义中。您可以拥有多个,也可以使用组合列的外键。
最后在构建SQL String时要小心,你的语句是:
CREATE TABLE if not exists PlayerStats (
_id integer PRIMARY KEY autoincrement,
PlayerId integer,
Goals_Scored,
Minutes_Played,
...
...缺少某些列的类型。
p.s。:这个应该可以使用,例如
private static final String TABLE_CREATE =
"CREATE TABLE if not exists " + TABLE_NAME + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement,"
ANOTHER_COLUMN + " text" +")";
private static final String SQLITE_CREATE =
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement," +
PLAYER_ID + " integer,"
KEY_SCORE + " text," +
KEY_MINUTES + " text," +
KEY_SUBIN + " text," +
KEY_SUBOUT + " text," +
KEY_BOOKING + " text,"
+" FOREIGN KEY("+PLAYER_ID+") REFERENCES "+TABLE_NAME+" ("+KEY_ROWID+"))";
希望我能帮上忙。
干杯!
答案 1 :(得分:0)
你必须在android sqlite中将主键的名称命名为_id。
答案 2 :(得分:0)
根据日志,你在“FOREIGN”附近有一个语法错误,它清楚“除了整数之后的逗号”之外,“PlayerId”和“integer”之间没有空格。此外,你没有提交[Goals_Scored,Minutes_Played,Substitute_In,Substitute_Out,Booked]的类型。
我建议您了解有关SQL查询的更多信息,您可以从在桌面上使用sqlite开始。