android - Won't insert values to database || won't run -
i made small changes , can't seem see error causing of this. appreciated
main
package com.androidhive.androidsqlite; import java.util.list; import android.app.activity; import android.os.bundle; import android.util.log; import android.widget.textview; public class androidass2sql extends activity { /** called when activity first created. */ textview text; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); databasehandler db = new databasehandler(this); /** * crud operations * */ // inserting contacts log.d("insert: ", "inserting .."); db.addcontact(new coms("gold", "100.00", "0")); db.addcontact(new coms("silver", "300.00", "3")); db.addcontact(new coms("wheat", "500.00", "4")); db.addcontact(new coms("sugar", "750.00", "0")); db.addcontact(new coms("coffee", "433.00", "9")); // reading contacts log.d("reading: ", "reading coms.."); list<coms> contacts = db.getallcontacts(); (coms cn : contacts) { string log = "id: "+cn.getid()+" ,name: " + cn.getname() + " ,phone: " + cn.getphonenumber() + " ,quantity: " + cn.getquan(); // writing contacts log log.d("name: ", log); } coms rando = db.getcontact(1); text = (textview) findviewbyid(r.id.textview2); text.settext(string.valueof(rando)); coms rando1 = db.getcontact(2); text = (textview) findviewbyid(r.id.textview4); text.settext(string.valueof(rando1)); coms rando2 = db.getcontact(3); text = (textview) findviewbyid(r.id.textview6); text.settext(string.valueof(rando2)); coms rando3 = db.getcontact(4); text = (textview) findviewbyid(r.id.textview8); text.settext(string.valueof(rando3)); coms rando4 = db.getcontact(5); text = (textview) findviewbyid(r.id.textview10); text.settext(string.valueof(rando4)); } }
com class
package com.androidhive.androidsqlite; public class coms { //private variables int _id; string _name; string _price; string _quan; // empty constructor public coms(){ } // constructor public coms(int id, string name, string price){ this._id = id; this._name = name; this._price = price; } // constructor public coms(string name, string price){ this._name = name; this._price = price; } // constructor public coms(string name){ this._name = name; } public coms(string name, string price, string quan){ this._name = name; this._price = price; this._quan = quan; } public coms(int id, string name, string price, string quan){ this._id = id; this._name = name; this._price = price; this._quan = quan; } @override public string tostring(){ return this._price; } // getting id public int getid(){ return this._id; } // setting id public void setid(int id){ this._id = id; } // getting name public string getname(){ return this._name; } // setting name public void setname(string name){ this._name = name; } // getting phone number public string getphonenumber(){ return this._price; } // setting phone number public void setphonenumber(string phone_number){ this._price = phone_number; } // getting name public string getquan(){ return this._quan; } // setting name public void setquan(string name){ this._quan = name; } }
db handler - of changes made in here, trying add quantity (quan)
package com.androidhive.androidsqlite; import java.util.arraylist; import java.util.list; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class databasehandler extends sqliteopenhelper { // static variables // database version private static final int database_version = 1; // database name private static final string database_name = "comstuff"; // contacts table name private static final string table_contacts = "coms"; // contacts table columns names private static final string key_id = "id"; private static final string key_name = "name"; private static final string key_price = "price"; private static final string key_quan = "quantity"; public databasehandler(context context) { super(context, database_name, null, database_version); } // creating tables @override public void oncreate(sqlitedatabase db) { string create_contacts_table = "create table " + table_contacts + "(" + key_id + " integer primary key," + key_name + " text," + key_price + " text," + key_quan + " text" + ")"; db.execsql(create_contacts_table); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // drop older table if existed db.execsql("drop table if exists " + table_contacts); // create tables again oncreate(db); } /** * crud(create, read, update, delete) operations */ // adding new contact void addcontact(coms contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, contact.getname()); // contact name values.put(key_price, contact.getphonenumber()); // contact phone values.put(key_quan, contact.getquan()); // contact phone // inserting row db.insert(table_contacts, null, values); db.close(); // closing database connection } // getting single contact coms getcontact(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_contacts, new string[] { key_id, key_name, key_price, key_quan }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); coms contact = new coms(integer.parseint(cursor.getstring(0)), cursor.getstring(1), cursor.getstring(2), cursor.getstring(3)); // return contact return contact; } coms getname(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_contacts, new string[] { key_name, key_price, key_quan }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); coms contact = new coms(integer.parseint(cursor.getstring(0)), cursor.getstring(1), cursor.getstring(2), cursor.getstring(3)); // return contact return contact; } // getting contacts public list<coms> getallcontacts() { list<coms> contactlist = new arraylist<coms>(); // select query string selectquery = "select * " + table_contacts; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { coms contact = new coms(); contact.setid(integer.parseint(cursor.getstring(0))); contact.setname(cursor.getstring(1)); contact.setphonenumber(cursor.getstring(2)); //when changed 2 3 causes error contact.setquan(cursor.getstring(3)); // adding contact list contactlist.add(contact); } while (cursor.movetonext()); } // return contact list return contactlist; } // updating single contact public int updatecontact(coms contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, contact.getname()); values.put(key_price, contact.getphonenumber()); values.put(key_quan, contact.getquan()); // updating row return db.update(table_contacts, values, key_id + " = ?", new string[] { string.valueof(contact.getid()) }); } // deleting single contact public void deletecontact(coms contact) { sqlitedatabase db = this.getwritabledatabase(); db.delete(table_contacts, key_id + " = ?", new string[] { string.valueof(contact.getid()) }); db.close(); } // getting contacts count public int getcontactscount() { string countquery = "select * " + table_contacts; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); cursor.close(); // return count return cursor.getcount(); } }
the database tables created in first run. if database created, oncreate not called again. if add new column table , insert record involving new column error.
either clear data or uninstall app , install , run again.
in emulator check 'wipe user data'.
Comments
Post a Comment