I've created the child table and set the proper primary keys and everything but when I try defining it as a foreign key in another table it doesn't recognize its existence. I'm still fairly new to SQLite and Java and I've pretty much just started and have been experimenting so if its some basics that I overlooked or something I completely missed I apologize.
Here's the onCreate snippet from my code which is bugging out:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+ termsTable +" ("+ TermsID + " INTEGER PRIMARY KEY , "+ TermsClass + " TEXT)");
db.execSQL("CREATE TABLE "+ statTable +" ("+ statID + " INTEGER PRIMARY KEY , "+ statClass + " TEXT)");
db.execSQL("CREATE TABLE "+ accountsTable +" ("+ colID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ colName +" TEXT, "+
colAmount +" Integer, "+
colPurpose +" TEXT, "+
colTerms + " Integer, "+ "FOREIGN KEY ("+ TermsID +") REFERENCES "+ termsTable +" ("+ TermsID +") " + "," +
colDate + " TEXT, " +
colStatus + " Integer, " + " FOREIGN KEY ("+ statID +") REFERENCES " + statTable +" ("+ statID +"));");
db.execSQL("CREATE TRIGGER fk_accterm_termid " +
" BEFORE INSERT "+
" ON "+ accountsTable +
" FOR EACH ROW BEGIN"+
" SELECT CASE WHEN ((SELECT "+ TermsID +" FROM "+ termsTable +" WHERE "+ TermsID +"=new."+ colTerms +" ) IS NULL)"+
" THEN RAISE (ABORT,'Foreign Key Violation') END;"+
" END;");
db.execSQL("CREATE TRIGGER fk_accstat_statid " +
" BEFORE INSERT "+
" ON "+ accountsTable +
" FOR EACH ROW BEGIN"+
" SELECT CASE WHEN ((SELECT "+ statID +" FROM "+ statTable +" WHERE "+ statID +"=new."+ colStatus +" ) IS NULL)"+
" THEN RAISE (ABORT,'Foreign Key Violation') END;"+
" END;");
db.execSQL("CREATE VIEW "+ viewAccs +
" AS SELECT "+ accountsTable +"."+colID+" AS _id,"+
" "+ accountsTable +"."+colName+","+
" "+ accountsTable +"."+ colAmount +","+
" "+ accountsTable +"."+ colPurpose + ","+
" "+ termsTable +"."+ TermsClass +""+
" FROM "+ accountsTable +" JOIN "+ termsTable +
" ON "+ accountsTable +"."+ colTerms +" ="+ termsTable +"."+ TermsID+
" "+ accountsTable +"."+ colDate +","+
" "+ statTable +"."+ statClass +""+
" FROM "+ accountsTable +" JOIN "+ statTable +
" ON "+ accountsTable +"."+ colStatus +" ="+ statTable +"."+ statID
);
InsertTerms(db);
InsertStatus(db);
}
and if it's needed my onUpgrade snippet:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+ accountsTable);
db.execSQL("DROP TABLE IF EXISTS "+ termsTable);
db.execSQL("DROP TABLE IF EXISTS "+ statTable);
db.execSQL("DROP TRIGGER IF EXISTS acc_id_trigger");
db.execSQL("DROP TRIGGER IF EXISTS acc_id_trigger22");
db.execSQL("DROP TRIGGER IF EXISTS fk_accdterm_termid");
db.execSQL("DROP TRIGGER IF EXISTS fk_accstat_statid");
db.execSQL("DROP VIEW IF EXISTS "+ viewAccs);
onCreate(db);
}
and the lovely logcat that comes with them:
02-14 01:20:01.882 3034-3034/com.example.jc.creditcradle E/SQLiteLog﹕ (1) unknown column "TermsID" in foreign key definition
02-14 01:20:01.897 3034-3034/com.example.jc.creditcradle D/AndroidRuntime﹕ Shutting down VM
02-14 01:20:01.924 3034-3034/com.example.jc.creditcradle E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.jc.creditcradle, PID: 3034
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.jc.creditcradle/com.example.jc.creditcradle.AccountManager}:
android.database.sqlite.SQLiteException: unknown column "TermsID" in foreign key definition
(code 1): , while compiling: CREATE TABLE Accounts (AccountID INTEGER PRIMARY KEY AUTOINCREMENT,
AccountName TEXT, Amount Integer, Purpose TEXT, Terms Integer,
FOREIGN KEY (TermsID) REFERENCES Terms (TermsID) ,DateCreated TEXT, Status Integer,
FOREIGN KEY (StatID) REFERENCES Status (StatID));
Again my apologies if this is a simple syntax error or beginners mistake, any and all help or feedback is well appreciated. Also I don't really know how to post a logcat properly so sorry for the misuse of the code snippet thing.
Aucun commentaire:
Enregistrer un commentaire