java - Checkbox and sql database update -
hi have address book app, added "favourite" checkbox in xml. im trying link checkbox sql database connector class in code, when new contact saved,updated,edited can update database. im having trouble because checkbox boolean other fields used contact strings, app stops runnning when use string checkbox. below database connector class, have indicated lines of code added.
// databaseconnector.java// provides easy connection , creation of usercontacts database. public class databaseconnector{ // database name private static final string database_name = "usercontacts"; private sqlitedatabase database; // database object private databaseopenhelper databaseopenhelper; // database helper // public constructor databaseconnector - other activity classes create 1 everytime need db access public databaseconnector(context context){ // create new databaseopenhelper (its inner class coded below extends sqliteopenhelper, see constructor constructors parameter semantics) databaseopenhelper = new databaseopenhelper(context, database_name, null, 1); }
// open database connection public void open() throws sqlexception{ //any error in method cause specified (imported) exception // create or open database reading/writing database = databaseopenhelper.getwritabledatabase(); //inherited sqliteopenhelper databaseopenhelper extends } // close database connection public void close(){ if (database != null) database.close(); //inherited sqliteopenhelper databaseopenhelper extends } // insert (add), update (edit) , delete not require display no cursor returned (in either case there return "intenting" activity save/delete(after confirm dialog) button pressed // inserts new contact in database public void insertcontact(string name, string email, string phone, string state, string city/*,boolean favourite*/){ contentvalues newcontact = new contentvalues(); // required parameter type sqlitedatabase.insert(...) - key/value data structure newcontact.put("name", name); newcontact.put("email", email); newcontact.put("phone", phone); newcontact.put("street", state); newcontact.put("city", city); newcontact.put("favourite",favourite); //code added open(); // open()coded above database.insert("contacts", null, newcontact); // parameters: table, not used here (has inserting empty records), contentvalues object close(); // close() coded above } // updates existing contact in database public void updatecontact(long id, string name, string email, string phone, string state, string city/*, boolean favourite*/){ contentvalues editcontact = new contentvalues(); // required parameter type sqlitedatabase.update(...) - key/value data structure editcontact.put("name", name); editcontact.put("email", email); editcontact.put("phone", phone); editcontact.put("street", state); editcontact.put("city", city); editcontact.put("favourite", favourite); // code added; open(); database.update("contacts", editcontact, "_id=" + id, null); // parameters: table, contentvalues object, clause, arguments (not used here allows compound conditions) close(); } // delete contact specified given string name public void deletecontact(long id){ open(); // parameters: table, clause without where, ... database.delete("contacts", "_id=" + id, null); // parameters: close(); } // viewing or 1 contact require data returned (via cursor) call point display // return cursor contact information in database public cursor getallcontacts(){ // parameters: table, columns in string array, ... other sql select statement stuff return database.query("contacts", new string[] {"_id", "name"}, null, null, null, null, "name"); } // cursor containing information contact specified given id public cursor getonecontact(long id){ // parameters: table, null = return columns, clause without where, ... other sql select statement stuff return database.query("contacts", null, "_id=" + id, null, null, null, null); } private class databaseopenhelper extends sqliteopenhelper{ public databaseopenhelper(context context, string name, cursorfactory factory, int version){ // if db schema version higher 1 on device supplied onupgrade run onupgrade upgrade schema appropriately (which must code of course) // new versions of app using new schema versions of db able update db schema , function correctly (without data loss) super(context, name, factory, version); // parameters: constructor call (context, database_name, null, 1) // null = use default cursor factory, 1 = version 1 of database wrt structure (schema) not data } // creates contacts table when database created @override //required public void oncreate(sqlitedatabase db){ //only executes if db not exist // query create new table named contacts // naming of column _id important since specifies record id used elsewhere when accessing table string createquery = "create table contacts" + "(_id integer primary key autoincrement," + "name text, email text, phone text," + "street text, city text);"; db.execsql(createquery); } @override //required public void onupgrade(sqlitedatabase db, int oldversion, int newversion){ } } }
convert boolean string
editcontact.put("favourite", favourite.tostring());
Comments
Post a Comment