android - having trouble with inserting data into mysqlite database -
hi new android development , need inserting data mysqlite database me understand problem using log cat , error gives me
05-02 18:53:05.087: i/database(331): sqlite returned: error code = 1, msg = table suspect has no column named other 05-02 18:53:05.097: e/database(331): error inserting other=8 – 14 nationality= height=8 – 14 age= name= suspect= gender=male hair=v@n
package com.example.sherlock; import java.util.list; import android.app.activity; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.spinner; public class database extends activity implements onclicklistener { private edittext nametxt; private spinner gender; private spinner age; private edittext hairtxt; private edittext heighttxt; private edittext nationalitytxt; private edittext othertxt; private spinner suspecttype; private button submitbtn; private button clearbtn; //called when activity created @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_database); nametxt =(edittext) findviewbyid(r.id.nametxt); gender =(spinner) findviewbyid(r.id.gender); age =(spinner) findviewbyid(r.id.age); hairtxt =(edittext) findviewbyid(r.id.hairtxt); heighttxt =(edittext) findviewbyid(r.id.heighttxt); nationalitytxt =(edittext) findviewbyid(r.id.nationalitytxt); othertxt =(edittext) findviewbyid(r.id.othertxt); suspecttype =(spinner) findviewbyid(r.id.age); submitbtn =(button)findviewbyid(r.id.submitbtn); submitbtn.setonclicklistener(this); clearbtn =(button)findviewbyid(r.id.clearbtn); clearbtn.setonclicklistener(this); } public void onclick(view v){ if(v.getid ()==r.id.submitbtn){ string providenametxt = nametxt.gettext().tostring(); string providegender = gender.getselecteditem().tostring(); string provideage = age.getselecteditem().tostring(); string providehairtxt = hairtxt.gettext().tostring(); string provideheighttxt = heighttxt.gettext().tostring(); string providenationalitytxt = nationalitytxt.gettext().tostring(); string provideothertxt = othertxt.gettext().tostring(); string providesuspecttype = suspecttype.getselecteditem().tostring(); dbadapter db = new dbadapter(this); db.addsuspect(new suspects( providenametxt, providegender, provideage, providehairtxt, provideheighttxt, providenationalitytxt, provideothertxt, providesuspecttype)); //reading contacts log.d("reading: ", "reading contacts.."); list<suspects> suspects = db.getallsuspects(); (suspects cn : suspects) { string log = "id: "+ cn.getid() +" ,name: " + cn.getname() +" ,gender: " + cn.getgender() +" ,age: " + cn.getage() +" ,hair: " + cn.gethair() +" ,height: " + cn.getheight() +" ,nationality: " + cn.getnationality() +" ,other: " + cn.getother() +" ,suspecttype: " + cn.getsuspect(); //writing contacts log log.d("name: ", log); } //release existing ui db.close(); finish(); } } } dbadapter
package com.example.sherlock; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import java.util.arraylist; import java.util.list; public class dbadapter extends sqliteopenhelper { public static final string key_rowid = "id"; public static final string key_name = "name"; public static final string key_gender = "gender"; public static final string key_height = "height"; public static final string key_age = "age"; public static final string key_hair = "hair"; public static final string key_nationality = "nationality"; public static final string key_suspect = "suspect"; public static final string key_other = "other"; private static final string database_name = "suspectdb"; private static final string table_suspects = "suspect"; private static final int database_version = 1; public dbadapter(context context) { super(context, database_name, null, database_version); } //create tables @override public void oncreate(sqlitedatabase db) { string create_suspects_table = "create table " + table_suspects + "(" + key_rowid + " integer primary key," + key_name + " text," + key_gender + " text," + key_height + "text," + key_age + "text," + key_hair + "text," + key_nationality + "text," + key_suspect + "text," + key_other + "text" +")"; db.execsql(create_suspects_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_suspects); // create tables again oncreate(db); } /** * crud(create, read, update, delete) operations */ // adding new suspect void addsuspect(suspects suspects) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, suspects.getname()); // contact name values.put(key_gender, suspects.getgender()); values.put(key_height, suspects.getheight()); values.put(key_age, suspects.getage()); values.put(key_hair, suspects.gethair()); values.put(key_nationality, suspects.getnationality()); values.put(key_suspect, suspects.getsuspect()); values.put(key_other, suspects.getother()); // inserting row db.insert(table_suspects, null, values); db.close(); // closing database connection } // getting single contact suspects getsuspect(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_suspects, new string[] { key_rowid, key_name, key_gender, key_height, key_age, key_hair,key_nationality,key_suspect, key_other }, key_rowid + "=?", new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); suspects suspect = new suspects(integer.parseint(cursor.getstring(0)), cursor.getstring(1), cursor.getstring(2), cursor.getstring(3), cursor.getstring(4), cursor.getstring(5), cursor.getstring(6), cursor.getstring(7), cursor.getstring(8)); // return suspect return suspect; } // getting suspects public list<suspects> getallsuspects() { list<suspects> suspectslist = new arraylist<suspects>(); // select query string selectquery = "select * " + table_suspects; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { suspects suspect = new suspects(); suspect.setid(integer.parseint(cursor.getstring(0))); suspect.setname(cursor.getstring(1)); suspect.setgender(cursor.getstring(2)); suspect.setheight(cursor.getstring(3)); suspect.setage(cursor.getstring(4)); suspect.sethair(cursor.getstring(5)); suspect.setnationality(cursor.getstring(6)); suspect.setsuspect(cursor.getstring(7)); suspect.setother(cursor.getstring(8)); // adding contact list suspectslist.add(suspect); } while (cursor.movetonext()); } // return suspect list return suspectslist; } // updating single suspect public int updatesuspects(suspects suspect) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, suspect.getname()); values.put(key_gender, suspect.getgender()); values.put(key_height, suspect.getheight()); values.put(key_age, suspect.getage()); values.put(key_hair, suspect.gethair()); values.put(key_nationality, suspect.getnationality()); values.put(key_suspect, suspect.getsuspect()); values.put(key_other, suspect.getother()); // updating row return db.update(table_suspects, values, key_rowid + " = ?", new string[] { string.valueof(suspect.getid()) }); } // deleting single suspect public void deletesuspect(suspects suspect) { sqlitedatabase db = this.getwritabledatabase(); db.delete(table_suspects, key_rowid + " = ?", new string[] { string.valueof(suspect.getid()) }); db.close(); } // getting suspect count public int getsuspectscount() { string countquery = "select * " + table_suspects; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); cursor.close(); // return count return cursor.getcount(); } }
fixed error had rename database suspectsdb
Comments
Post a Comment