Insert Data to android sqlite database doesn't work -
hey i'm trying insert data sqlite db, when click insert button nothing happens no force close, no errors,no successful/unsuccessful toast message nothing @ all.and when click view db info button no data inserted db.i did earlier , had no problem can't find i've gone wrong time please me out thank in advance.
mainactivity
package com.example.sql; import android.app.activity; import android.app.dialog; import android.content.intent; import android.media.mediaplayer; 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.textview; import android.widget.toast; public class mainactivity extends activity implements onclicklistener { button insert,viewd; edittext name,age; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); name = (edittext)findviewbyid(r.id.etname); age = (edittext) findviewbyid(r.id.etage); insert = (button)findviewbyid(r.id.btninsert); viewd = (button)findviewbyid(r.id.btnview); insert.setonclicklistener(this); viewd.setonclicklistener(this); } @override public void onclick(view v) { // todo auto-generated method stub final mediaplayer mpbuttonclick = mediaplayer.create(this, r.raw.buttonclick); switch (v.getid()) { case r.id.btninsert: boolean status = true; try{ string name1 = name.gettext().tostring(); string age1 = age.gettext().tostring(); sqlactivity entry = new sqlactivity(mainactivity.this); entry.open(); entry.passdatatoentry(name1,age1); entry.close(); mpbuttonclick.start(); } catch (exception e) { // todo: handle exception status=false; string error = e.tostring(); log.e("exception found ",error); } finally{ if(status) { toast.maketext(getapplicationcontext(), "successfully inserted", toast.length_short).show(); } } break; case r.id.btnview: intent = new intent("com.example.sql.view"); startactivity(i); break; } } }
sqlactivity
package com.example.sql; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class sqlactivity { public static final string key_rowid = "_id"; public static final string key_name = "p_name"; public static final string key_age = "p_age"; private static final string database_name = "sqldb"; private static final string database_table = "data_table"; private static final int database_version = 1; private dbhelper ourhelper; private final context ourcontext; private sqlitedatabase ourdatabase; private static class dbhelper extends sqliteopenhelper { public dbhelper(context context) { super(context, database_name, null, database_version); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql("create table " + database_table + " (" + key_rowid + " integer primary key autoincrement , " + key_name + " text not null , " + key_age + " text not null);" ); string y = "create table " + database_table + " (" + key_rowid + " integer primary key autoincrement , " + key_name + " text not null , " + key_age + " text not null);"; //system.out.println("query" + y); log.d("query", y); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub log.w("taskdbadapter", "upgrading version " + oldversion + " " + newversion + ", destroy old data"); db.execsql("drop table if exists " + database_table); oncreate(db); } } public sqlactivity(context c) { ourcontext = c; } public sqlactivity open() throws sqlexception { ourhelper = new dbhelper(ourcontext); ourdatabase = ourhelper.getwritabledatabase(); return this; } public void close() { ourhelper.close(); } public long passdatatoentry(string name1, string age1) { // todo auto-generated method stub contentvalues cv = new contentvalues(); cv.put(key_name, name1); cv.put(key_age, age1); int = (int)ourdatabase.insert(database_table, null, cv); string my_tag=""; log.d(my_tag,"value= "+i); return ourdatabase.insert(database_table, null, cv); } public string getdata() { // todo auto-generated method stub string[] columns = new string[]{key_name,key_age}; cursor c = ourdatabase.query(database_table, columns, null, null, null, null, null); string result = ""; int irow = c.getcolumnindex(key_rowid); int iname = c.getcolumnindex(key_name); int iage = c.getcolumnindex(key_age); for(c.movetofirst(); !c.isafterlast(); c.movetonext()) { result = result + c.getstring(iname) + " " + c.getstring(iage) + "\n"; } return result; } }
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="28dp" android:text="name" android:textappearance="?android:attr/textappearancemedium" /> <edittext android:id="@+id/etname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="14dp" android:ems="10" android:inputtype="textpersonname" /> <textview android:id="@+id/tvinfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="40dp" android:text="age" android:textappearance="?android:attr/textappearancemedium" /> <edittext android:id="@+id/etage" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputtype="number" /> <button android:id="@+id/btninsert" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="130dp" android:text="insert" /> <button android:id="@+id/btnview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="view" /> </linearlayout>
logcat
04-18 22:43:50.625: e/zygote(33): setreuid() failed. errno: 2 04-18 22:43:56.095: e/zygote(33): setreuid() failed. errno: 17 04-18 22:43:56.985: e/batteryservice(59): usbonlinepath not found 04-18 22:43:56.985: e/batteryservice(59): batteryvoltagepath not found 04-18 22:43:56.985: e/batteryservice(59): batterytemperaturepath not found 04-18 22:43:56.995: e/surfaceflinger(59): couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake 04-18 22:44:01.455: e/eventhub(59): not driver version /dev/input/mouse0, not typewriter 04-18 22:44:01.455: e/eventhub(59): not driver version /dev/input/mice, not typewriter 04-18 22:44:02.105: e/system(59): failure starting core service 04-18 22:44:02.105: e/system(59): java.lang.securityexception 04-18 22:44:02.105: e/system(59): @ android.os.binderproxy.transact(native method) 04-18 22:44:02.105: e/system(59): @ android.os.servicemanagerproxy.addservice(servicemanagernative.java:146) 04-18 22:44:02.105: e/system(59): @ android.os.servicemanager.addservice(servicemanager.java:72) 04-18 22:44:02.105: e/system(59): @ com.android.server.serverthread.run(systemserver.java:184) 04-18 22:44:02.665: e/soundpool(59): error loading /system/media/audio/ui/effect_tick.ogg 04-18 22:44:02.675: e/soundpool(59): error loading /system/media/audio/ui/keypressstandard.ogg 04-18 22:44:02.675: e/soundpool(59): error loading /system/media/audio/ui/keypressspacebar.ogg 04-18 22:44:02.675: e/soundpool(59): error loading /system/media/audio/ui/keypressdelete.ogg 04-18 22:44:02.675: e/soundpool(59): error loading /system/media/audio/ui/keypressreturn.ogg 04-18 22:44:03.755: e/throttleservice(59): not open gps configuration file /etc/gps.conf 04-18 22:44:05.005: e/logwrapper(147): executing /system/bin/tc failed: no such file or directory 04-18 22:44:05.045: e/logwrapper(148): executing /system/bin/tc failed: no such file or directory 04-18 22:44:05.065: e/logwrapper(149): executing /system/bin/tc failed: no such file or directory 04-18 22:44:10.423: e/hierarchicalstatemachine(59): tethermaster - unhandledmessage: msg.what=3
viewactivity
package com.example.sql; import android.app.activity; import android.os.bundle; import android.widget.textview; public class viewactivity extends activity{ @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.view); textview tv = (textview)findviewbyid(r.id.tvinfo); sqlactivity info = new sqlactivity(this); info.open(); info.getdata(); } }
view.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="data...." /> <textview android:id="@+id/tvinfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="db info" /> </linearlayout>
set text in viewactiviy
tv.settext(info.getdata()); info.close();
Comments
Post a Comment