我在m registr活动中编写此代码,但它给出了错误ANR并且没有生成数据库
注册按钮上的这段代码
SQLiteDatabase db = openOrCreateDatabase("RoseDetails",MODE_APPEND, null);
// db.execSQL("create table if not exists table_register(firstname VARCHAR(20),lastname VARCHAR(20),emailaddress VARCHAR(20),gender VARCHAR(20),password VARCHAR(20),confirmpassword VARCHAR(20)");
// db.execSQL("INSERT into table_student values('"+firstname.getText()+"','"+lastname.getText()+"','"+emailid.getText()+"','"+gendervaule+"','"+Pwd.getText()+"','"+"','"+cpwd.getText()+"')");
// Cursor c=db.rawQuery("SELECT * FROM table_register" , null);
// while(c.moveToNext())
// {
// if(c.moveToFirst())
// {
// String fn=c.getString(c.getColumnIndex("firstname"));
// String ln=c.getString(c.getColumnIndex("lastname"));
// String add=c.getString(c.getColumnIndex("emailaddress"));
// String g=c.getString(c.getColumnIndex("gender"));
// String p1=c.getString(c.getColumnIndex("password"));
// String p2=c.getString(c.getColumnIndex("confirmpassword"));
// Toast.makeText(this,"firstname"+fn+"\t"+"lastname"+ln+"\t"+"emailid"+add+"\t"+"gender"+g+"\t"+"pass"+p1+"\t"+"cpwd"+p2,Toast.LENGTH_LONG).show();
// }
// }
// c.close();
// db.close();
logcat错误
11-24 08:01:00.163: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-24 08:01:00.193: E/AndroidRuntime(278): FATAL EXCEPTION: main
11-24 08:01:00.193: E/AndroidRuntime(278): android.database.sqlite.SQLiteException: near ")": syntax error: create table if not exists table_register(firstname VARCHAR(20),lastname VARCHAR(20),emailaddress VARCHAR(20),gender VARCHAR(20),password VARCHAR(20),confirmpassword VARCHAR(20)
11-24 08:01:00.193: E/AndroidRuntime(278): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
11-24 08:01:00.193: E/AndroidRuntime(278): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)
11-24 08:01:00.193: E/AndroidRuntime(278): at com.example.orderplacemnet.Registration_Page.onClick(Registration_Page.java:158)
11-24 08:01:00.193: E/AndroidRuntime(278): at android.view.View.performClick(View.java:2408)
11-24 08:01:00.193: E/AndroidRuntime(278): at android.view.View$PerformClick.run(View.java:8816)
11-24 08:01:00.193: E/AndroidRuntime(278): at android.os.Handler.handleCallback(Handler.java:587)
11-24 08:01:00.193: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:92)
11-24 08:01:00.193: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
11-24 08:01:00.193: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-24 08:01:00.193: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 08:01:00.193: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
11-24 08:01:00.193: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-24 08:01:00.193: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-24 08:01:00.193: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您忘了在末尾的这一行添加右括号
db.execSQL("create table if not exists table_register(firstname VARCHAR(20),lastname VARCHAR(20),emailaddress VARCHAR(20),gender VARCHAR(20),password VARCHAR(20),confirmpassword VARCHAR(20)");
应该是
db.execSQL("create table if not exists table_register(firstname VARCHAR(20),lastname VARCHAR(20),emailaddress VARCHAR(20),gender VARCHAR(20),password VARCHAR(20),confirmpassword VARCHAR(20))");
答案 1 :(得分:0)
最佳做法是先在SQLITE工具上执行查询,然后再放入代码。它将防止任何类型的语法错误。