Wednesday, 15 February 2012

java - where did i go wrong SQLiteConstraintException: error code 19: constraint failed -



java - where did i go wrong SQLiteConstraintException: error code 19: constraint failed -

i know been asked bunch of times, need help here. i'm trying insert info unfortunately came across problem. please help me. here's codes. here's dbadapter.

public class dbadapter { private static final string database_name = "my3db.db"; private static final string database_table = "classes"; private static final string database_table_student = "student"; private static final int database_version = 2; public static final string tag = "dbadapter"; public static final string key_id = "_id"; public static final string key_subject = "subject"; public static final string key_schedule = "schedule"; public static final string key_room = "room"; public static final string key_note = "note"; public static final string stud_id = "_id"; public static final string stud_name = "name"; public static final string stud_course = "course"; public static final string stud_swrk = "swrk"; public static final string stud_assn = "assn"; public static final string stud_proj = "proj"; public static final string stud_exam = "exam"; public static final string stud_classid = "classid_stud"; public static final string view_stud = "view_stud"; private static final string database_create = "create table classes (_id integer primary key autoincrement, " + "subject text not null, schedule text not null, room text not null, note text not null);"; private static final string database_create_student = "create table pupil (_id integer primary key autoincrement, " + "name text not null, course of study text not null, swrk text not null, assn text not null, proj text not null, exam text not null, classid_stud integer, foreign key(classid_stud) references classes(_id))"; private final context context; private databasehelper dbhelper; private sqlitedatabase db; public dbadapter(context ctx) { this.context = ctx; dbhelper = new databasehelper(context); } private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub // seek { db.execsql(database_create); db.execsql(database_create_student); db.execsql("create trigger fk_studclass_classid " + " before insert "+ " on "+database_table_student+ " each row begin"+ " select case when ((select "+key_id+" "+database_table+" "+key_id+"=new."+stud_classid+" ) null)"+ " raise (abort,'foreign key violation') end;"+ " end;"); db.execsql("create view "+view_stud+ " select "+database_table_student+"."+stud_id+" _id,"+ " "+database_table_student+"."+stud_name+","+ " "+database_table_student+"."+stud_course+","+ " "+database_table_student+"."+stud_swrk+","+ " "+database_table_student+"."+stud_assn+","+ " "+database_table_student+"."+stud_proj+","+ " "+database_table_student+"."+stud_exam+","+ " "+database_table+"."+key_subject+","+ " "+database_table+"."+key_schedule+","+ " "+database_table+"."+key_room+","+ " "+database_table+"."+key_note+""+ " "+database_table_student+" bring together "+database_table+ " on "+database_table_student+"."+stud_classid+" ="+database_table+"."+key_id ); // } grab (sqlexception e) { // e.printstacktrace(); // } } @override public void onopen(sqlitedatabase db) { // todo auto-generated method stub super.onopen(db); if (!db.isreadonly()) { // enable foreign key constraints db.execsql("pragma foreign_keys=on;"); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub log.w(tag, "upgrading database version" + oldversion + "to" + newversion + ", destroy old data"); db.execsql("drop table if exists classes"); db.execsql("drop table if exists student"); db.execsql("drop trigger if exists class_id_trigger"); db.execsql("drop trigger if exists class_id_trigger22"); db.execsql("drop trigger if exists fk_studclass_classid"); db.execsql("drop view if exists "+view_stud); oncreate(db); } } public void addclass(string subject, string schedule, string room, string note) { contentvalues cv = new contentvalues(); cv.put(key_subject, subject); cv.put(key_schedule, schedule); cv.put(key_room, room); cv.put(key_note, note); db.insert(database_table, key_subject, cv); db.close(); } public void insertsomestudents() { addstudent("james", "come", "yes", "no", "maybe", "probably"); addstudent("joross", "nursing", "bla", "ba", "black", "sheep"); }

here's code activity. it's not done, insert code there. bear it.

public class overview extends activity { spinner spinner; dbadapter db = null; private simplecursoradapter dataadapter; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.overview); db = new dbadapter(this); spinner = (spinner) findviewbyid(r.id.spinner); listview listview = (listview) findviewbyid(r.id.overlist); loadspinnerclassoverview(); db.open(); //db.deleteallstudents(); db.insertsomestudents(); db.open(); displaylistview(); listview.setadapter(dataadapter); db.close(); } private void displaylistview() { cursor cursor = db.getallstudent(); dataadapter = new simplecursoradapter(this, r.layout.overviewlist, cursor, // string[] columns = new string[] { dbadapter.stud_name, dbadapter.stud_course // dbadapter.stud_swrk, // dbadapter.stud_assn, // dbadapter.stud_proj, // dbadapter.stud_exam, }, // int[] = new int[] { r.id.name, r.id.course, // r.id.swrk, // r.id.assn, // r.id.proj, // r.id.exam }); } private void loadspinnerclassoverview() { // database handler dbadapter db = new dbadapter(getapplicationcontext()); // spinner drop downwards elements list<string> listclass = db.getlistclass(); // creating adapter spinner arrayadapter<string> dataadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, listclass); // drop downwards layout style - list view radio button dataadapter .setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); // attaching info adapter spinner spinner.setadapter(dataadapter); }

logcat

error inserting course=come swrk=yes exam=probably proj=maybe assn=no name=james android.database.sqlite.sqliteconstraintexception: error code 19: constraint failed

java android sqlite

No comments:

Post a Comment