lundi 13 avril 2015

my foreign key isnt working?

I am having an issue with my tables, the first table creates a reference to the data in the second table, the CARD_ID increments fine, but the foreign key in CARD_INFO_TABLE isnt autoincrementing or even realising it is a foreign key of an autoincrement, So i would just like to know, where am I going wrong!?



public static final String CREATE_CARD_TABLE = "CREATE TABLE "
+ CARD_TABLE + "(" + CARD_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + CARD_NAME
+ " TEXT )";

public static final String CREATE_CARDINFO_TABLE = "CREATE TABLE "
+ CARD_INFO_TABLE + "(" + CARD_ID2
+ " INTEGER, " + CARD_NAME2
+ " TEXT, " + CARD_WEIGHT
+ " REAL, " + CARD_REPS
+ " INTEGER, "
+ " FOREIGN KEY " + "("+ CARD_ID2 +")"+ " REFERENCES " + CARD_TABLE + "("+CARD_ID+"));";


and then I insert data to them as follows, where am I going wrong??



public void insertEntry(String name, double weight, int reps) {
if(flag == 0) {
open();
database.beginTransaction();
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put(DataBaseHelper.CARD_NAME, name);

// Insert the row into your table
database.insert(DataBaseHelper.CARD_TABLE, null, newValues);
flag = 1;
database.setTransactionSuccessful();
database.endTransaction();
close();
}
open();
database.beginTransaction();
ContentValues newValues2 = new ContentValues();
// Assign values for each row.
newValues2.put(DataBaseHelper.CARD_NAME2, name);
newValues2.put(DataBaseHelper.CARD_WEIGHT, weight);
newValues2.put(DataBaseHelper.CARD_REPS, reps);

// Insert the row into your table
database.insert(DataBaseHelper.CARD_INFO_TABLE, null, newValues2);
database.setTransactionSuccessful();
database.endTransaction();
close();
}

Aucun commentaire:

Enregistrer un commentaire