0

由于 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 吗?

4

3 回答 3

1

这是我从SQLite 文档中获取的修改示例:

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 字符串时要小心,您的语句:

CREATE TABLE if not exists PlayerStats (
 _id        integer PRIMARY KEY autoincrement,
  PlayerId  integer,
  Goals_Scored,
  Minutes_Played,
  ...

...缺少某些列的类型。

ps:这个应该可以工作,例如

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

希望我能帮上一点忙。

干杯!

于 2013-04-18T20:09:24.713 回答
0

根据日志,您在“FOREIGN”附近有一个语法错误,很明显“PlayerId”和“integer”之间没有空格,除了整数后面的逗号。此外,您没有提交 [Goals_Scored,Minutes_Played,Substitute_In,Substitute_Out,Booked] 的类型。

我建议学习更多关于 SQL 查询的知识,你可以从在桌面上使用 sqlite 开始。

于 2013-04-18T20:10:42.033 回答
0

您必须在 android sqlite 中将主键的名称命名为 _id。

于 2013-04-18T19:57:38.133 回答