android - Foreign Key Constraint Failed 787 causing abort on attempt to insert entry into table -
trying insert accounts
table. data collected fine , thing runs smooth until after confirms account added. @ point operation aborted due foreign key constraint failure shown here :
logcat
04-01 22:58:58.750 23053-23053/? e/sqlitelog﹕ (787) abort @ 37 in [insert accounts(terms,amount,accountname,datecreated,status,balance,purpose,payperiod) values (?,?,?,?,?,?,?,?)]: foreign key constraint failed 04-01 22:58:58.999 23053-23053/? e/sqlitedatabase﹕ error inserting terms=0 amount=10000 accountname=acc 1 datecreated=04-01-2015 22:58:58 status=1 balance=10000 purpose=test payperiod=1
i've pulled dbfile , opened check if tables accounts
referenced we're populating , are. i've ran database schema in sqlfiddle see fails , ran without problems.
now i'm lost , don't know should looking for, if can point me in direction of mistake appreciated.
database helper
oncreate
@override public void oncreate(sqlitedatabase db) { db.execsql("create table " + termstable + " (" + coltermsid + " integer primary key , " + coltermsclass + " text)"); db.execsql("create table " + periodtable + " (" + colperiodid + " integer primary key , " + colperiodclass + " text)"); db.execsql("create table " + stattable + " (" + colstatusid + " integer primary key , " + colstatclass + " text)"); db.execsql("create table " + accountstable + " (" + colid + " integer primary key autoincrement, " + colname + " text, " + colamount + " integer, " + colpurpose + " text, " + colterms + " integer not null, " + colperiod +" integer not null, " + colbalance +" integer, "+ colstatus + " integer default '1'," + coldate + " text, " + coleditdate + " text, " + "foreign key (" + colterms + ") references " + termstable + " (" + coltermsid + ") " + "," + "foreign key (" + colperiod + ") references " + periodtable + " (" + colperiodid + ") " + "," + "foreign key (" + colstatus + ") references " + stattable + " (" + colstatusid + "));"); db.execsql("create table " + paytable + " (" + colpayid + " integer primary key , " + colgroupid + " integer not null, " + colpaybal + " text, " + colinterest + " text, " + colpaydue + " text, " + coldatedue + " text, " + colpaid + " integer, " + "foreign key (" + colgroupid + ") references " + accountstable + " (" + colid + ") on delete cascade);"); db.execsql("create view " + viewaccs + " select " + accountstable + "." + colid + " _id," + " " + accountstable + "." + colname + "," + " " + accountstable + "." + colamount + "," + " " + accountstable + "." + colpurpose + "," + " " + termstable + "." + coltermsclass + "," + " " + periodtable + "." + colperiodclass + "," + " " + accountstable+ "." + colbalance + "," + " " + stattable + "." + colstatclass + "," + " " + accountstable + "." + coldate + "," + " " + accountstable + "." + coleditdate + "" + " " + accountstable + " join " + termstable + " on " + accountstable + "." + colterms + " = " + termstable + "." + coltermsid + " join " + periodtable + " on " + accountstable + "." + colperiod + " = " + periodtable + "." + colperiodid + " join " + stattable + " on " + accountstable + "." + colstatus + " = " + stattable + "." + colstatusid ); db.execsql("create view " + viewpmnts + " select " + paytable + "." + colpayid + " _id," + " " + accountstable + "." + colid + "," + " " + paytable + "." + colgroupid + "," + " " + paytable + "." + colpaybal + "," + " " + paytable + "." + colinterest + "," + " " + paytable + "." + colpaydue + "," + " " + paytable + "." + coldatedue + "," + " " + paytable + "." + colpaid + "" + " " + paytable + " join " + accountstable + " on " + paytable + "." + colgroupid + " = " + accountstable + "." + colid ); insertterms(db); insertperiods(db); insertstatus(db); }
addaccount
( method within databasehelper )
void addaccount(account acc) { sqlitedatabase db = this.getwritabledatabase(); contentvalues cv = new contentvalues(); cv.put(colname, acc.getname()); cv.put(colamount, acc.getamt()); cv.put(colpurpose, acc.getpurpose()); cv.put(colterms, acc.getterms()); cv.put(colperiod, acc.getperiod()); cv.put(colbalance, acc.getamt()); cv.put(coldate, acc.getdate()); cv.put(colstatus, acc.getstatus()); db.insert(accountstable, colname, cv); db.close(); }
based on values trying insert, there must termstable row id 0, stattable row id 1, , periodtable row id 1. if 1 of rows not exist foreign key constraint error. guess don't have terms row id 0.
Comments
Post a Comment