lundi 4 avril 2016

SQLite insert id from previous insert to populate value of column in another table

I am a newcomer to android programming and SQLite and have come across a hurdle. I have 3 tables in question; teams, leagues and a lookup table called teams_vs_leagues which holds the id of a team and the id of a league so they are linked. Below are the tables designs:

//Create teams Table
private static final String CREATE_TEAMS_TABLE = "CREATE TABLE "
        + TEAMS_TABLE + "("
        + ID + " INTEGER PRIMARY KEY,"
        + TEAM_NAME + " TEXT,"
        + IMAGE + " TEXT" + ")";

//Create leagues Table
private static final String CREATE_LEAGUES_TABLE = "CREATE TABLE "
        + LEAGUES_TABLE + "("
        + ID + " INTEGER PRIMARY KEY,"
        + LEAGUE_NAME + " TEXT" + ")";

//Create teams_vs_leagues Table
private static final String CREATE_TEAMS_VS_LEAGUES_TABLE = "CREATE TABLE "
        + TEAMS_VS_LEAGUES_TABLE + "("
        + ID + " INTEGER PRIMARY KEY,"
        + TEAM_ID + " INTEGER,"
        + LEAGUE_ID + " INTEGER,"
        + POINTS + " INTEGER"+ ")";

Below is currently the method I have of adding a new team:

public boolean createTeam(String team_name, String image, String team_id, String league_id, String points) {
 SQLiteDatabase db = this.getWritableDatabase();

 ContentValues values = new ContentValues();

 values.put(TEAM_NAME, team_name);
 values.put(IMAGE, image);

 long result = db.insert(TEAMS_TABLE, null, values);

 if (result == -1)
  return false;
 else
  return true;
}

How can I add TEAM_ID into the teams_vs_league table as the id from the insert above? Any help would be great appreciated.

Aucun commentaire:

Enregistrer un commentaire