android - runtime error about FriendsDBHelper -
this logcat error
08-20 10:37:52.463: e/database(1361): failure 1 (near "create_tablefriends": syntax error) on 0x9f418b8 when preparing 'create_tablefriends(id integer primary key autoincremaent,fname text(20), lname text(30), nickanme text(20))'. 08-20 10:37:52.481: d/androidruntime(1361): shutting down vm 08-20 10:37:52.481: w/dalvikvm(1361): threadid=1: thread exiting uncaught exception (group=0xb60084f0) 08-20 10:37:52.527: e/androidruntime(1361): fatal exception: main 08-20 10:37:52.527: e/androidruntime(1361): java.lang.runtimeexception: unable start activity componentinfo{com.example.sqlite/com.example.sqlite.insertactivity}: android.database.sqlite.sqliteexception: near "create_tablefriends": syntax error: create_tablefriends(id integer primary key autoincremaent,fname text(20), lname text(30), nickanme text(20)) 08-20 10:37:52.527: e/androidruntime(1361): @ android.app.activitythread.performlaunchactivity(activitythread.java:1647) 08-20 10:37:52.527: e/androidruntime(1361): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 08-20 10:37:52.527: e/androidruntime(1361): @ android.app.activitythread.access$1500(activitythread.java:117) 08-20 10:37:52.527: e/androidruntime(1361): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 08-20 10:37:52.527: e/androidruntime(1361): @ android.os.handler.dispatchmessage(handler.java:99) 08-20 10:37:52.527: e/androidruntime(1361): @ android.os.looper.loop(looper.java:130) 08-20 10:37:52.527: e/androidruntime(1361): @ android.app.activitythread.main(activitythread.java:3683) 08-20 10:37:52.527: e/androidruntime(1361): @ java.lang.reflect.method.invokenative(native method) 08-20 10:37:52.527: e/androidruntime(1361): @ java.lang.reflect.method.invoke(method.java:507) 08-20 10:37:52.527: e/androidruntime(1361): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 08-20 10:37:52.527: e/androidruntime(1361): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 08-20 10:37:52.527: e/androidruntime(1361): @ dalvik.system.nativestart.main(native method) 08-20 10:37:52.527: e/androidruntime(1361): caused by: android.database.sqlite.sqliteexception: near "create_tablefriends": syntax error: create_tablefriends(id integer primary key autoincremaent,fname text(20), lname text(30), nickanme text(20)) 08-20 10:37:52.527: e/androidruntime(1361): @ android.database.sqlite.sqlitedatabase.native_execsql(native method) 08-20 10:37:52.527: e/androidruntime(1361): @ android.database.sqlite.sqlitedatabase.execsql(sqlitedatabase.java:1763) 08-20 10:37:52.527: e/androidruntime(1361): @ com.example.sqlite.db.friendsdbhelper.oncreate(friendsdbhelper.java:30) 08-20 10:37:52.527: e/androidruntime(1361): @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:126) 08-20 10:37:52.527: e/androidruntime(1361): @ com.example.sqlite.db.friendsdb.<init>(friendsdb.java:19) 08-20 10:37:52.527: e/androidruntime(1361): @ com.example.sqlite.insertactivity.oncreate(insertactivity.java:43) 08-20 10:37:52.527: e/androidruntime(1361): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 08-20 10:37:52.527: e/androidruntime(1361): @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 08-20 10:37:52.527: e/androidruntime(1361): ... 11 more
this code form frienddbhelper
package com.example.sqlite.db; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class friendsdbhelper extends sqliteopenhelper { public static string table_name ="friends"; public static int table_version = 1; public static string table_key_id = "id"; public static string table_key_fname = "fname"; public static string table_key_lname = "lname"; public static string table_key_nickname = "nickanme"; public static string table_cerate_sql = "create_table"+table_name+"("+"" + ""+table_key_id+" integer primary key autoincremaent,"+ ""+table_key_fname+" text(20), "+ ""+table_key_lname+" text(30), "+ ""+table_key_nickname+" text(20)"+ ")"; public friendsdbhelper(context context) { super(context, table_name, null, table_version); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { db.execsql(table_cerate_sql); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table"+table_name); oncreate(db); log.i("friend db helper","table upgraded "+oldversion+"to"+newversion); }
}
this code when insert data sqlite
package com.example.sqlite; import java.security.publickey; import javax.xml.datatype.duration; import com.example.sqlite.db.friendsdb; import com.example.sqlite.db.friendsdbhelper; import android.app.activity; import android.content.context; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class insertactivity extends activity { private context context; private friendsdb db; edittext inputfname; edittext inputlname; edittext inputnickname; button save; button cancle; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.insert_layout); //view matching inputfname = (edittext)findviewbyid(r.id.fname); inputlname = (edittext)findviewbyid(r.id.lname); inputnickname = (edittext)findviewbyid(r.id.nickname); save = (button)findviewbyid(r.id.save); cancle = (button)findviewbyid(r.id.cancle); //context context = this; db = new friendsdb(context); //button event save.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { intent insertdata = new intent(insertactivity.this,friendslistactivity.class); insertdata.putextra("fname", inputfname.gettext().tostring()); insertdata.putextra("lname", inputlname.gettext().tostring()); insertdata.putextra("nickname", inputnickname.gettext().tostring()); if(insertdata!=null){ //insert data long recordid = db.insert(inputfname.gettext().tostring(), inputlname.gettext().tostring(), inputnickname.gettext().tostring()); startactivity(insertdata); } } } );
if want more information please tell me. thank you
i think there syntax error
in create table sentence, erase " _
" , put space
after table
, , change autoincremaent
autoincrement
this:
public static string table_cerate_sql = "create table "+table_name+" ("+"" + ""+table_key_id+" integer primary key autoincrement,"+ ""+table_key_fname+" text(20), "+ ""+table_key_lname+" text(30), "+ ""+table_key_nickname+" text(20)"+ ")";
Comments
Post a Comment