我试图在我的Android项目中使用SQLite,我的应用程序在插入虚拟数据时崩溃。
public void insertVehicle(){
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PetContract.FeedEntry.COLUMN_OWNER_NAME,"Tiki");
values.put(PetContract.FeedEntry.COLUMN_REGISTRATION_NUMBER,"DL154K5555");
values.put(PetContract.FeedEntry.COLUMN_TYPE,"Car");
long newRowId = db.insert(PetContract.FeedEntry.TABLE_NAME,null,values);
Log.v("CatalogActivity ","new Row id: "+newRowId);
}
public void onSubmitEntry(View view) {
getvalues();
getNotification();
// Toast.makeText(this, "Submit button clicked_selected_CAR", Toast.LENGTH_SHORT).show();
insertVehicle();
Intent intent = new Intent(this, Check_In_Page.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(uniqueId, notification.build());
}
FeedEntry类是
public static class FeedEntry implements BaseColumns{
public static final String TABLE_NAME = "Vehicle entry";
public static final String COLUMN_REGISTRATION_NUMBER = "Registration number";
public static final String COLUMN_OWNER_NAME = "Owner name";
public static final String COLUMN_TYPE = "Vehicle type";
public static final int TYPE_UNKNOWN = 0;
public static final int TYPE_MOTORBIKE = 1;
public static final int TYPE_CAR = 2;
}
stacktrace
04-09 19:36:31.018 16884-16884/com.example.android.parking12 W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
04-09 19:36:31.023 16884-16884/com.example.android.parking12 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.parking12, PID: 16884
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3848)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3843)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "entry": syntax error (code 1): , while compiling: CREATE TABLE Vehicle entry (_id INTEGER PRIMARY KEY AUTOINCREMENT, Registration number TEXT, Owner name TEXT, Vehicle type TEXT )
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1697)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1628)
at com.example.android.parking12.Date.FeedReaderDbHelper.onCreate(FeedReaderDbHelper.java:29)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.android.parking12.Check_In_Page.insertVehicle(Check_In_Page.java:67)
at com.example.android.parking12.Check_In_Page.onSubmitEntry(Check_In_Page.java:84)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3843)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我注意到你的表名和列名(字符串文字)中都有空格。您是否在实际查询中使用单引号创建表(使用反斜杠转义它们)?
编辑(在发布堆栈跟踪后)
我能够通过将代码拼接到一个简单的应用程序中来重新创建错误。该错误肯定来自表/列名称中的空格。
基于堆栈跟踪中的这一行:
2017/04/09
2个解决方案:
您的表/列名称必须是所有单词文字:
Caused by: android.database.sqlite.SQLiteException: near "entry": syntax error (code 1): , while compiling: CREATE TABLE Vehicle entry (_id INTEGER PRIMARY KEY AUTOINCREMENT, Registration number TEXT, Owner name TEXT, Vehicle type TEXT )
或者您需要为表/列名称的所有字符串文字添加单引号:
public static final String TABLE_NAME = "vehicleEntry";
public static final String COLUMN_REGISTRATION_NUMBER = "registrationNumber";
public static final String COLUMN_OWNER_NAME = "ownerName";
public static final String COLUMN_TYPE = "vehicleType";