android - Simple Sqlite Helper -


here today learning sqlite insert , view inserted records. when click save button got error "unfortunately appname stopped". can help?

sqlitedatabase helper

public class sqlitedatabasehelper extends sqliteopenhelper {  public static final string database_name="reg"; public static final string table_name="user_details"; public static final string column_id="id"; public static final string column_name="name"; public static final string column_email="email"; public static final string column_uname="uname"; public static final string column_password="pass"; public static final int database_version=1;   public sqlitedatabasehelper(context context){     super(context,database_name,null,database_version);  }  @override public void oncreate(sqlitedatabase db) {     string sql="create table"+table_name+"("+column_id+"integer primary key auto increment"+column_name+"varchar"+column_email+"varchar"             +column_uname+"varchar"+column_password+"varchar);";     db.execsql(sql);     log.d("create","table created sucessfully"); }  @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {      string sql="drop table if exist user_details ";     db.execsql(sql);     oncreate(db); } public boolean adduser(string name,string email,string uname,string pass) {      sqlitedatabase db=this.getwritabledatabase();      contentvalues contentvalues=new contentvalues();       contentvalues.put(column_name,name);     contentvalues.put(column_email,email);     contentvalues.put(column_uname,uname);     contentvalues.put(column_password,pass);      db.insert(table_name, null, contentvalues);     db.close();     return true; } public cursor getuser(int id) {     sqlitedatabase db=this.getreadabledatabase();     string sql="select * user_details id="+id+"; ";     cursor cursor= db.rawquery(sql,null);     return cursor; } 

mainactivity

enter code herepublic class mainactivity extends appcompatactivity  implements view.onclicklistener{  private edittext edname; private edittext edemail; private edittext eduname; private edittext edpass; private button bsave; private button bview;  sqlitedatabasehelper db;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      edname= (edittext) findviewbyid(r.id.fullname);     edemail= (edittext) findviewbyid(r.id.email);     eduname= (edittext) findviewbyid(r.id.uname);     edpass= (edittext) findviewbyid(r.id.pass);     bsave= (button) findviewbyid(r.id.save);     bview= (button) findviewbyid(r.id.view);      db= new sqlitedatabasehelper(this);     bsave.setonclicklistener(this);     bview.setonclicklistener(this);  } @override public void onclick(view v) {     if (v==bsave){         insert();       }     if (v==bview){         intent intent= new intent(mainactivity.this,viewuser.class);         startactivity(intent);      }  }  public void insert() {      string name=edname.gettext().tostring().trim();     string email=edemail.gettext().tostring().trim();     string unmae=eduname.gettext().tostring().trim();     string upass=edpass.gettext().tostring().trim();      db.adduser(name,email,unmae,upass);       toast.maketext(this,"new records inserted sucessfully",toast.length_long).show(); } 

viewuser

public class viewuser extends appcompatactivity {

private edittext dispfullname; private edittext dispemail; private edittext dispuname; private edittext dispass; private edittext dispuid; private button show; private button home;  sqlitedatabasehelper db;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_view_user);      dispuid= (edittext) findviewbyid(r.id.uid);     dispfullname= (edittext) findviewbyid(r.id.fullnamedisp);     dispemail= (edittext) findviewbyid(r.id.emaildisp);     dispuname= (edittext) findviewbyid(r.id.unamedisp);     dispass= (edittext) findviewbyid(r.id.passdisp);     show= (button) findviewbyid(r.id.show);     home= (button) findviewbyid(r.id.home);     db=  new sqlitedatabasehelper(this);     show.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             showuser(integer.parseint(dispuid.gettext().tostring().trim()));         }     });     home.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent intent= new intent(viewuser.this,mainactivity.class);             startactivity(intent);         }     });  }  private void showuser(int id) {     cursor c =db.getuser(id);     c.movetofirst();     {         if (c!=null){             dispuid.settext(c.getstring(c.getcolumnindex(sqlitedatabasehelper.column_id)));             dispfullname.settext(c.getstring(c.getcolumnindex(sqlitedatabasehelper.column_name)));             dispemail.settext(c.getstring(c.getcolumnindex(sqlitedatabasehelper.column_email)));             dispuname.settext(c.getstring(c.getcolumnindex(sqlitedatabasehelper.column_uname)));             dispass.settext(c.getstring(c.getcolumnindex(sqlitedatabasehelper.column_password)));           }     }  } 

log:

09-21 19:38:52.261  16080-16080/com.example.karthi.sqlitedesign e/androidruntime﹕ fatal exception: main process: com.example.karthi.sqlitedesign, pid: 16080 android.database.cursorindexoutofboundsexception: index 0 requested, size of 0         @ android.database.abstractcursor.checkposition(abstractcursor.java:426)         @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:136)         @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:50)         @ com.example.karthi.sqlitedesign.viewuser.showuser(viewuser.java:63)         @ com.example.karthi.sqlitedesign.viewuser.access$100(viewuser.java:14)         @ com.example.karthi.sqlitedesign.viewuser$1.onclick(viewuser.java:43)         @ android.view.view.performclick(view.java:4661)         @ android.view.view$performclick.run(view.java:19498)         @ android.os.handler.handlecallback(handler.java:733)         @ android.os.handler.dispatchmessage(handler.java:95)         @ android.os.looper.loop(looper.java:146)         @ android.app.activitythread.main(activitythread.java:5641)         @ java.lang.reflect.method.invokenative(native method)         @ java.lang.reflect.method.invoke(method.java:515)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1288)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1104)         @ dalvik.system.nativestart.main(native method) 09-21 19:39:17.715  17307-17407/? d/show﹕ read firstrelease/release_text assets 09-21 19:39:42.500  17785-17997/? d/show﹕ read firstrelease/release_text assets 09-21 19:39:55.252  18211-18293/? d/show﹕ read firstrelease/release_text assets 

courtesy opiatefuchs @karthickyadav

you missing sqlite create table statement .

whats fault

  1. use autoincrement instead of auto increment
  2. use text instead of varchar.

before create database please read android sqlite database tutorial , sqlitedatabase

dummy example

string create_contacts_table = "create table " + table_taxonomy + "("             + key_id + " integer primary key , " + key_taxonomy_code + " text, "             + key_display_name+" text )";     db.execsql(create_contacts_table); 

edit

string create_contacts_table = "create table " + table_name + "(" + column_id + " integer primary key autoincrement, " + column_name + " text, "+ column_email +"text," + column_uname + "text," + column_password +" text )"; db.execsql(create_contacts_table);  

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -