2017-04-09 68 views
0

我試圖在我的Android項目中使用SQLite,並且我的應用程序在插入虛擬數據時崩潰。應用程序崩潰在Android Studio中使用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; 

} 

堆棧跟蹤

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)  
+1

你創建表?請發佈您的堆棧跟蹤。 – peter

+0

您應該始終發佈您的堆棧跟蹤 –

+0

嘗試更改數據庫的版本。大多數新手都會犯錯誤。 – Queendevelopers

回答

0

我注意到,你的表和列名(字符串文字)在他們的空間。您是否在實際查詢中使用單引號創建表(使用反斜槓轉義它們)?

編輯:(堆棧跟蹤被張貼後)

我能夠重新拼接你的錯誤在你的代碼放到一個簡單的應用程序。該錯誤肯定來自表格/列名稱中的空格。

基於這一行從堆棧跟蹤:

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) 

2解決方案:

  1. 無論你的表/列名必須是一個詞的文字:

    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"; 
    
  2. 或者您需要添加單引號t O表/列名的所有字符串文字:

    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'";