java - Android Database isn't inserting data -
so have following piece of code, code trys add data database , if can't closes app. no matter app closes, advice
package net.connormccarthy.walkingdeadcharacterprofiles; import java.util.arraylist; import android.app.activity; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.graphics.color; import android.graphics.typeface; 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.tablelayout; import android.widget.tablerow; import android.widget.textview; public class rickaddnotes extends activity { sqlitedatabase db; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { // try catch block returns better error reporting log // android specific calls super.oncreate(savedinstancestate); setcontentview(r.layout.ricknotes); button btnleft=(button)findviewbyid(r.id.btnricksave); // create database manager object //db = new mainactivity(); final button button = (button) findviewbyid(r.id.btnricksave); button.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { data(); } }); } public long data() { long d=0; //edittext edittext1=(edittext )findviewbyid(r.id.edittext1); //string notes = edittext1.tostring(); try { final string insert_data="insert characters values(2,'woop','5')"; db.execsql(insert_data); } catch (exception e) { system.exit(0); } return d; } public void showdata(view view) { cursor c=db.rawquery("select * character character = rick", null); int count= c.getcount(); c.movetofirst(); tablelayout tablelayout = new tablelayout(getapplicationcontext()); tablelayout.setverticalscrollbarenabled(true); tablerow tablerow; textview textview,textview1,textview2,textview3,textview4,textview5; tablerow = new tablerow(getapplicationcontext()); textview=new textview(getapplicationcontext()); textview.settext("firstname"); textview.settextcolor(color.red); textview.settypeface(null, typeface.bold); textview.setpadding(20, 20, 20, 20); tablerow.addview(textview); textview4=new textview(getapplicationcontext()); textview4.settext("lastname"); textview4.settextcolor(color.red); textview4.settypeface(null, typeface.bold); textview4.setpadding(20, 20, 20, 20); tablerow.addview(textview4); textview5=new textview(getapplicationcontext()); textview5.settext("email"); textview5.settextcolor(color.red); textview5.settypeface(null, typeface.bold); textview5.setpadding(20, 20, 20, 20); tablerow.addview(textview5); tablelayout.addview(tablerow); (integer j = 0; j < count; j++) { tablerow = new tablerow(getapplicationcontext()); textview1 = new textview(getapplicationcontext()); textview1.settext(c.getstring(c.getcolumnindex("character"))); textview2 = new textview(getapplicationcontext()); textview2.settext(c.getstring(c.getcolumnindex("notes"))); textview1.setpadding(20, 20, 20, 20); textview2.setpadding(20, 20, 20, 20); tablerow.addview(textview1); tablerow.addview(textview2); tablelayout.addview(tablerow); c.movetonext() ; } setcontentview(tablelayout); db.close(); } }
if dont add exception app crashes. want work variable pass (edittext1) when hard code data app crashes or closes, depending on weather or not add exception.
any appreciated.
in sql statement, viewing 2 types of probable errors -
1>
final string insert_data="insert characters values(2,'woop','5')";
here table name characters
where in
cursor c=db.rawquery("select * character character = rick", null);
your table name character
please check this.
2> select query should -
cursor c=db.rawquery("select * character character = rick", null);
should -
cursor c=db.rawquery("select * character character = 'rick'", null);
please @ these 2 pointers. further comments.
thanks
Comments
Post a Comment