android - Insert And Select in SQlite database -
i want enter date in edit text field ,that should stored in table clicking save button . after select data should displayed in second page how it.please me.
here code
database helper.java:
public class databasehelper extends sqliteopenhelper { public databasehelper(context context) { super(context, database_name, null, database_version); } // logcat tag private static final string log = "databasehelper"; // database version private static final int database_version = 1; // database name private static final string database_name = "motolifedatabase"; // table names private static final string table_fluidhistory = "fluidhistory"; //fluid history-column names private static final string fluidchange_id = "fluidchageid"; private static final string fcvehicle_id = "vehicalid"; private static final string fcfluid_id = "fluidid"; private static final string last_change = "lastchangedate"; private static final string next_change = "nextchangedate"; private static final string last_changeodo = "lastchangeodo"; private static final string next_changeodo = "nextchangeodo"; // table create statements //fluidchange table stament private static final string create_table_fluidchange = "create table " + table_fluidhistory + "(" + fluidchange_id + " integer primary key," + fcvehicle_id + " integer"+ fcfluid_id + "integer" + "date" +next_change +"date"+last_changeodo +"integer" + next_changeodo +"integer" +");"; @override public void oncreate(sqlitedatabase db) { // creating required tables db.execsql(create_table_fluidchange); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // on upgrade drop older tables db.execsql("drop table if exists " + table_fluidhistory); // create new tables oncreate(db); } enter code here public void addfluidhistory(fluidhistory fluidhistory) { // todo auto-generated method stub sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(last_change, fluidhistory.getlastchangedate()); // first name values.put(next_change, fluidhistory.getnextchangedate()); values.put(last_changeodo, fluidhistory.getlastchangeodo()); values.put(next_changeodo, fluidhistory.getnextchangeodo());// last name // inserting row db.insert(table_fluidhistory , null, values); // closing database connection db.close(); } public list<fluidhistory> getfluidhistory() { // todo auto-generated method stub list<fluidhistory> namevolist = new arraylist<fluidhistory>(); // select query string selectquery = "select * " + table_fluidhistory; sqlitedatabase db1 = this.getwritabledatabase(); cursor cursor = db1.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { fluidhistory namevo = new fluidhistory(); namevo.setlastchangedate(cursor.getstring(0)); namevo.setnextchangedate(cursor.getstring(1)); namevo.setlastchangeodo(cursor.getstring(2)); namevo.setnextchangeodo(cursor.getstring(3)); // adding namevo list namevolist.add(namevo); } while (cursor.movetonext()); } // return namevo list return namevolist; } } main activity class:
her main activity class code please refer code , me how change .
import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class mainactivity extends actionbaractivity { private edittext edt1,edt2,edt3,edt4; private databasehelper handler; @suppresswarnings("unused") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.first_screen); // stringbuffer details=new stringbuffer(); textview names=(textview)findviewbyid(r.id.textview1); final edittext edt1=(edittext)findviewbyid(r.id.edittext1); final edittext edt2=(edittext)findviewbyid(r.id.edittext2); final edittext edt3=(edittext)findviewbyid(r.id.edittext3); final edittext edt4=(edittext)findviewbyid(r.id.edittext4); button btn=(button)findviewbyid(r.id.save); final databasehelper handler=new databasehelper(this); btn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub handler.addfluidhistory(new fluidhistory(edt1.gettext().tostring(),edt2.gettext().tostring(),edt3.gettext().tostring(),edt4.gettext().tostring())); } }); } }
its not working me.after entering values click save button application closed .why happen can know please me.thank in advance.
update logcat:
06-03 02:16:44.695: e/androidruntime(1102): fatal exception: main 06-03 02:16:44.695: e/androidruntime(1102): process: com.example.tejuraju, pid: 1102 06-03 02:16:44.695: e/androidruntime(1102): java.lang.nullpointerexception 06-03 02:16:44.695: e/androidruntime(1102): @ com.example.tejuraju.mainactivity.onclick(mainactivity.java:57) 06-03 02:16:44.695: e/androidruntime(1102): @ android.view.view.performclick(view.java:4438) 06-03 02:16:44.695: e/androidruntime(1102): @ android.view.view$performclick.run(view.java:18422) 06-03 02:16:44.695: e/androidruntime(1102): @ android.os.handler.handlecallback(handler.java:733) 06-03 02:16:44.695: e/androidruntime(1102): @ android.os.handler.dispatchmessage(handler.java:95) 06-03 02:16:44.695: e/androidruntime(1102): @ android.os.looper.loop(looper.java:136) 06-03 02:16:44.695: e/androidruntime(1102): @ android.app.activitythread.main(activitythread.java:5017) 06-03 02:16:44.695: e/androidruntime(1102): @ java.lang.reflect.method.invokenative(native method) 06-03 02:16:44.695: e/androidruntime(1102): @ java.lang.reflect.method.invoke(method.java:515) 06-03 02:16:44.695: e/androidruntime(1102): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 06-03 02:16:44.695: e/androidruntime(1102): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 06-03 02:16:44.695: e/androidruntime(1102): @ dalvik.system.nativestart.main(native method)
for saving data there many options.
you possibly use shared preference if going 1 date value.
reference: http://www.vogella.com/tutorials/androidfilebasedpersistence/article.html
if going temporary value needed in next activity, can use pass value via intent.
reference: http://www.vogella.com/tutorials/androidintent/article.html
if want save value in db (advisable multiple records/table structure), can use sql lite
reference:http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
Comments
Post a Comment