sqlite - Android Cant execute Update Query -
i little new in so, trying execute update query change value of field on basis of selection made in radio group. giving me nullpointerexception , havent been able figure out cause yet. code:
genderfragment:
package com.example.yipeedo; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.button; import android.widget.radiobutton; import android.widget.radiogroup; public class genderfragment extends fragment { int mcurrentpage; button next; radiogroup gendergroup; int x; string choice; radiobutton gender; view v; public sqlitedatabase db; databasehelper dbh; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /** getting arguments bundle object */ bundle data = getarguments(); /** getting integer data of key current_page bundle */ mcurrentpage = data.getint("current_page", 0); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { v = inflater.inflate(r.layout.gender_layout, container,false); next=(button)v.findviewbyid(r.id.next); gendergroup=(radiogroup)v.findviewbyid(r.id.gendergroup); next.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub updatedatabase(); ((preferencesactivity)getactivity()).setcurrentitem(1, true); } }); return v; } public void updatedatabase() { int selectedid=gendergroup.getcheckedradiobuttonid(); gender=(radiobutton)v.findviewbyid(selectedid); string selection=gender.gettext().tostring(); log.e("add gender database","gender:"+selection); try { //dbh.createdatabase(); //dbh.opendatabase(); dbh.updategender(selection); } catch(exception e) { e.printstacktrace(); } } }
databasehelper:
package com.example.yipeedo; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class databasehelper extends sqliteopenhelper{ private static string db_path = "/data/data/com.example.yipeedo/databases/"; private static string db_name = "yipeedodb1.txt"; private sqlitedatabase mydatabase; private final context mycontext; inputstream is; public databasehelper(context context) { super(context, db_name, null, 1); this.mycontext = context; } public void write(inputstream is) { try { outputstream out = new fileoutputstream(new file(db_path)); int read = 0; byte[] bytes = new byte[1024]; while ((read = is.read(bytes)) != -1) { out.write(bytes, 0, read); } is.close(); out.flush(); out.close(); system.err.println(out + "\n"); } catch (ioexception e) { e.printstacktrace(); } } @override public void oncreate(sqlitedatabase db) { try { is=mycontext.getassets().open("yipeedodb1.txt"); write(is); mydatabase.openorcreatedatabase(db_path+db_name, null); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } public void updategender(string x) { mydatabase.execsql("update userdata set gender = " + x + " name 'ram';"); } }
logcat:
08-18 22:34:46.088: w/system.err(17086): java.lang.nullpointerexception 08-18 22:34:46.108: w/system.err(17086): @ com.example.yipeedo.genderfragment.updatedatabase(genderfragment.java:71) 08-18 22:34:46.108: w/system.err(17086): @ com.example.yipeedo.genderfragment$1.onclick(genderfragment.java:51) 08-18 22:34:46.108: w/system.err(17086): @ android.view.view.performclick(view.java:4432) 08-18 22:34:46.108: w/system.err(17086): @ android.view.view$performclick.run(view.java:18338) 08-18 22:34:46.108: w/system.err(17086): @ android.os.handler.handlecallback(handler.java:725) 08-18 22:34:46.108: w/system.err(17086): @ android.os.handler.dispatchmessage(handler.java:92) 08-18 22:34:46.108: w/system.err(17086): @ android.os.looper.loop(looper.java:137) 08-18 22:34:46.108: w/system.err(17086): @ android.app.activitythread.main(activitythread.java:5283) 08-18 22:34:46.108: w/system.err(17086): @ java.lang.reflect.method.invokenative(native method) 08-18 22:34:46.108: w/system.err(17086): @ java.lang.reflect.method.invoke(method.java:511) 08-18 22:34:46.108: w/system.err(17086): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) 08-18 22:34:46.108: w/system.err(17086): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) 08-18 22:34:46.108: w/system.err(17086): @ dalvik.system.nativestart.main(native method)
the problem variable dbh, null.
on oncreate method add:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /** getting arguments bundle object */ bundle data = getarguments(); /** getting integer data of key current_page bundle */ mcurrentpage = data.getint("current_page", 0); dbh = new databasehelper(getactivity()); }
and modify:
public void updategender(string x) { if(mydatabase == null) mydatabase = getwritabledatabase(); mydatabase.execsql("update userdata set gender = " + x + " name 'ram';"); }
and need copy database asset internal storage before call oncreate method of dbopenhelper, please reconsider class implementation. here example of implementation what best practices sqlite on android?.
Comments
Post a Comment