android - Foreign Key Syntax Error Table not being created -


my table not being created because of foreign key syntax error cant seem figure out. made sure created int first before called fk. here's teh code class 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 + ")" ; 

the primary key in class

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)'.

is because i'm not inserting player_id on oncreate method?

this modified example took 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) ); 

as can see, have add foreign key after column definitions - don't try put them inside column definitions. can have more one, , foreign keys combined columns.

finally when building sql string, statement:

create table if not exists playerstats (  _id        integer primary key autoincrement,   playerid  integer,   goals_scored,   minutes_played,   ... 

... missing types columns.

p.s.: 1 should work, instance

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

hope bit.

cheers!


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -