java - show data in textview from sqlite database in android -


this question has answer here:

i'm trying show data in textviews database in android show run time erroe.. create database file as..

package com.example.databasetest;   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.provider.basecolumns; import android.util.log;  public class dbhelper extends sqliteopenhelper{      private static final int database_version = 1;     private static final string database_name = "teacher";     private static final string table_name = "teacher_table";      private static final string name = "teacher_name";     private static final string father_name = "father_name";     private static final string mother_name = "mother_name";      sqlitedatabase data=this.getwritabledatabase();      context ctx;        public dbhelper(context context) {         super(context, database_name, null, database_version);         ctx=context;         log.d("database operation", "database created");     }      @override     public void oncreate(sqlitedatabase db) {         data=db;           db.execsql("create table " + table_name + " ("                     + basecolumns._id + " id integer primary key autoincrement, "                     + name + " text,"                     + father_name + " text,"                     + mother_name + " text,"                     + ");");         log.d("database operation", "table created");       }      public void open() throws sqlexception      {          dbhelper db1 = new dbhelper(ctx);             data = db1.getwritabledatabase();      }       public void close()      {          data.close();      }     @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         db.execsql("drop table if exception exists");         oncreate(db);            }     public void oninsert(dbhelper db,string name,string f_name, string m_name)     {         sqlitedatabase sql= db.getwritabledatabase();         contentvalues cv=new contentvalues();         cv.put("teacher_name",name);         cv.put("father_name", f_name);         cv.put("mother_name", m_name);         sql.insert(table_name, null, cv);         log.d("database operation", "one row inserted.....");     }      public cursor getinformation(dbhelper dop)     {         sqlitedatabase sq=dop.getreadabledatabase();         string[] coloumns={name,father_name,mother_name};         cursor cr=sq.query(table_name, coloumns, null, null, null, null, null);         return cr;      }   } 

and java file as...

package com.example.databasetest;    import java.util.arraylist; import java.util.list;  import android.app.activity; import android.content.context; import android.database.cursor; import android.os.bundle; import android.widget.textview;  public class showdetail extends activity{     textview name,fathername,mothername;     string name,fathername,mothername;     context ctxx=this;     @override     protected void oncreate(bundle savedinstancestate) {         // todo auto-generated method stub         super.oncreate(savedinstancestate);         setcontentview(r.layout.show_data);         name=(textview)findviewbyid(r.id.name);         fathername=(textview)findviewbyid(r.id.father_name);         mothername=(textview)findviewbyid(r.id.mother_name);          list<string> list = new arraylist<string>();         dbhelper db= new dbhelper(ctxx);         db.open();         cursor cr=db.getinformation(db);         if (cr.movetofirst()) {          {               list.add(cr.getstring(0));               name=cr.getstring(0);               fathername=cr.getstring(1);               mothername=cr.getstring(2);             } while (cr.movetonext());       }       if (cr != null && !cr.isclosed()) {          cr.close();       }       if(db!=null)       {           db.close();       }        name.settext(name);       fathername.settext(fathername);       mothername.settext(mothername);       } } 

and log cate show error as.....

05-07 06:25:59.173: d/gralloc_goldfish(918): emulator without gpu emulation detected. 05-07 06:26:15.483: e/sqlitelog(918): (1) autoincrement allowed on integer primary key 05-07 06:26:15.483: d/androidruntime(918): shutting down vm 05-07 06:26:15.483: w/dalvikvm(918): threadid=1: thread exiting uncaught exception (group=0x41465700) 05-07 06:26:15.553: e/androidruntime(918): fatal exception: main 05-07 06:26:15.553: e/androidruntime(918): android.database.sqlite.sqliteexception: autoincrement allowed on integer primary key (code 1): , while compiling: create table teacher_table (_id id integer primary key autoincrement, teacher_name text,father_name text,mother_name text,); 05-07 06:26:15.553: e/androidruntime(918):  @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) 05-07 06:26:15.553: e/androidruntime(918):  @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:889) 05-07 06:26:15.553: e/androidruntime(918):  @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:500) 05-07 06:26:15.553: e/androidruntime(918):  @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) 05-07 06:26:15.553: e/androidruntime(918):  @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) 05-07 06:26:15.553: e/androidruntime(918):  @ android.database.sqlite.sqlitestatement.<init>(sqlitestatement.java:31) 05-07 06:26:15.553: e/androidruntime(918):  @ android.database.sqlite.sqlitedatabase.executesql(sqlitedatabase.java:1672) 05-07 06:26:15.553: e/androidruntime(918):  @ android.database.sqlite.sqlitedatabase.execsql(sqlitedatabase.java:1603) 05-07 06:26:15.553: e/androidruntime(918):  @ com.example.databasetest.dbhelper.oncreate(dbhelper.java:38) 05-07 06:26:15.553: e/androidruntime(918):  @ android.database.sqlite.sqliteopenhelper.getdatabaselocked(sqliteopenhelper.java:252) 05-07 06:26:15.553: e/androidruntime(918):  @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:164) 05-07 06:26:15.553: e/androidruntime(918):  @ com.example.databasetest.dbhelper.<init>(dbhelper.java:23) 05-07 06:26:15.553: e/androidruntime(918):  @ com.example.databasetest.mainactivity$1.onclick(mainactivity.java:46) 05-07 06:26:15.553: e/androidruntime(918):  @ android.view.view.performclick(view.java:4240) 05-07 06:26:15.553: e/androidruntime(918):  @ android.view.view$performclick.run(view.java:17721) 05-07 06:26:15.553: e/androidruntime(918):  @ android.os.handler.handlecallback(handler.java:730) 05-07 06:26:15.553: e/androidruntime(918):  @ android.os.handler.dispatchmessage(handler.java:92) 05-07 06:26:15.553: e/androidruntime(918):  @ android.os.looper.loop(looper.java:137) 05-07 06:26:15.553: e/androidruntime(918):  @ android.app.activitythread.main(activitythread.java:5103) 05-07 06:26:15.553: e/androidruntime(918):  @ java.lang.reflect.method.invokenative(native method) 05-07 06:26:15.553: e/androidruntime(918):  @ java.lang.reflect.method.invoke(method.java:525) 05-07 06:26:15.553: e/androidruntime(918):  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 05-07 06:26:15.553: e/androidruntime(918):  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 05-07 06:26:15.553: e/androidruntime(918):  @ dalvik.system.nativestart.main(native method) 

please me fix error

your create table sql command wrong. corrected below

 db.execsql("create table " + table_name + " ("                 + basecolumns._id + " integer primary key autoincrement, "                 + name + " text,"                 + father_name + " text,"                 + mother_name + " text"                 + ");"); 

Comments