下面是我的Android应用程序代码。创建一个存储名称和电子邮件的数据库。 但是,当我在Android的模拟器中运行该程序时它不起作用,并说我的应用程序已经停止。 我的代码中没有出现任何阻止我运行它的错误 请帮忙 。
String fname , lname, email;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=openOrCreateDatabase("MYDB1",MODE_PRIVATE , null);
db.execSQL("CREATE TABLE IF NOT TEXISTS Student(fname VARCHAR , lname VARCHAR , email VARCHAR);");
}
private void Adddata(View view)
{
EditText edittext1=(EditText) findViewById(R.id.firstname);
EditText edittext2=(EditText) findViewById(R.id.lastname);
EditText edittext3=(EditText) findViewById(R.id.email);
fname=edittext1.getText().toString();
lname=edittext2.getText().toString();
email=edittext3.getText().toString();
db.execSQL("INSERT INTO Student Values('"+fname+"', '"+lname+"','"+email+"');");
}
private void Showdata(View view)
{
Cursor c=db.rawQuery("SELECT * from Student", null);
int count =c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView,textView1,textView2,textView3,textView4,textView5;
tableRow = new TableRow(getApplicationContext());
textView = new TextView(getApplicationContext());
textView.setText("FirstName");
textView.setTextColor(Color.RED);
textView.setTypeface(null,Typeface.BOLD);
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
textView4=new TextView(getApplicationContext());
textView4.setText("LastName");
textView4.setTextColor(Color.RED);
textView4.setTypeface(null,Typeface.BOLD);
textView4.setPadding(20, 20, 20, 20);
tableRow.addView(textView4);
textView5=new TextView(getApplicationContext());
textView5.setText("Email");
textView5.setTextColor(Color.RED);
textView5.setTypeface(null,Typeface.BOLD);
textView5.setPadding(20, 20, 20, 20);
tableRow.addView(textView5);
tableLayout.addView(tableRow);
for (Integer j = 0; j< count; j++)
{
tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("fname")));
textView2 = new TextView(getApplicationContext());
textView2.setText(c.getString(c.getColumnIndex("lname")));
textView3 = new TextView(getApplicationContext());
textView3.setText(c.getString(c.getColumnIndex("email")));
textView1.setPadding(20, 20, 20,20);
textView2.setPadding(20, 20, 20,20);
textView3.setPadding(20, 20, 20,20);
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tableLayout.addView(tableRow);
c.moveToNext();
}
setContentView(tableLayout);
db.close();
}
public void close(View view)
{
System.exit(0);
}
这是我的logcat
04-15 05:33:35.650: D/dalvikvm(929): Not late-enabling CheckJNI (already on)
04-15 05:33:37.460: E/SQLiteLog(929): (1) near "TEXISTS": syntax error
04-15 05:33:37.460: D/AndroidRuntime(929): Shutting down VM
04-15 05:33:37.460: W/dalvikvm(929): threadid=1: thread exiting with uncaught exception (group=0xb4aebba8)
04-15 05:33:37.470: E/AndroidRuntime(929): FATAL EXCEPTION: main
04-15 05:33:37.470: E/AndroidRuntime(929): Process: com.example.studentdatabase, PID: 929
04-15 05:33:37.470: E/AndroidRuntime(929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.studentdatabase/com.example.studentdatabase.MainActivity}: android.database.sqlite.SQLiteException: near "TEXISTS": syntax error (code 1): , while compiling: CREATE TABLE IF NOT TEXISTS Student(fname VARCHAR , lname VARCHAR , email VARCHAR);
04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.os.Looper.loop(Looper.java:136)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 05:33:37.470: E/AndroidRuntime(929): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 05:33:37.470: E/AndroidRuntime(929): at java.lang.reflect.Method.invoke(Method.java:515)
04-15 05:33:37.470: E/AndroidRuntime(929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 05:33:37.470: E/AndroidRuntime(929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 05:33:37.470: E/AndroidRuntime(929): at dalvik.system.NativeStart.main(Native Method)
04-15 05:33:37.470: E/AndroidRuntime(929): Caused by: android.database.sqlite.SQLiteException: near "TEXISTS": syntax error (code 1): , while compiling: CREATE TABLE IF NOT TEXISTS Student(fname VARCHAR , lname VARCHAR , email VARCHAR);
04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
04-15 05:33:37.470: E/AndroidRuntime(929): at com.example.studentdatabase.MainActivity.onCreate(MainActivity.java:26)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.Activity.performCreate(Activity.java:5231)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-15 05:33:37.470: E/AndroidRuntime(929): ... 11 more
答案 0 :(得分:0)
它是一个错字。 TEXTISTS =&gt;存在。
答案 1 :(得分:0)
阅读你的stacktrace:
android.database.sqlite.SQLiteException:near“TEXISTS”:语法错误(代码1):,编译时:CREATE TABLE IF NOT TEXISTS Student(fname VARCHAR,lname VARCHAR,email VARCHAR);
将TEXISTS
更改为EXISTS
,它应该会更好。