android - I have problems with Database inserting in eclispe -
i've created simple app student's name , save in database , if inserted , show toast otherwise show 'didn't work' toast. how can find out database saved?
here script codes:
student
public class student { public string name; }
databasemanager
import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class databasemanager extends sqliteopenhelper { public databasemanager(context context) { super(context, "mydata", null, 1); } @override public void oncreate(sqlitedatabase arg0) { // todo auto-generated method stub string query = "create table tbl_student_info ( " + " name varchar( 10 )," + ");"; arg0.execsql(query); } @override public void onupgrade(sqlitedatabase arg0, int arg1, int arg2) { // todo auto-generated method stub string query = " drop table if exists tbl_student_info "; arg0.execsql(query); oncreate(arg0); } public boolean addstudent(student student) { boolean result; try { string query = "insert tbl_student_info (name, tel)" + " values ('" + student.name + "')"; sqlitedatabase db = this.getwritabledatabase(); db.execsql(query); db.close(); result = true; } catch (exception ex) { result = false; } return result; }
mainactivity
import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class testdatabasesimpletestactivity extends activity { edittext edittext1; button button1; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); settitle(" registry page "); edittext1 = (edittext) findviewbyid(r.id.edittext1); button1 = (button) findviewbyid(r.id.button1); button1.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { string name = edittext1.gettext().tostring(); student student = new student(); student.name = name; databasemanager db = new databasemanager(getapplicationcontext()); boolean result = db.addstudent(student); if (result == true) { string s = "it works..."; toast.maketext(getapplicationcontext(), s, toast.length_long).show(); } else { string s = "did not works..."; toast.maketext(getapplicationcontext(), s, toast.length_long).show(); } } }); }
main
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name :" /> <edittext android:id="@+id/edittext1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestfocus /> </edittext> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="insert" /> </linearlayout>
Comments
Post a Comment