2014-01-26 124 views
1

我想通過它更新/刪除一個項目id。當按鈕被選中時,項目不會更新或刪除

當我在「更新按鈕」上選擇後,嘗試我的代碼時, 沒有任何更新。

當我嘗試「刪除按鈕」時,也沒有任何東西被刪除。

我想根據它ID刪除我的項目。 我在androidOpenDBHelper中使用basecolumns._ID。

logcat的爲我的更新部分:

01-27 01:13:39.676: E/AndroidRuntime(29370): FATAL EXCEPTION: main 
01-27 01:13:39.676: E/AndroidRuntime(29370): android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: UPDATE fuelLog SET tcost=?,fuelprice=?,fcon=?,odometer=?,date=?,preodometer=?,fuelpump=? WHERE _id=? AND date+=? 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1038) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:649) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1563) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1514) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at com.example.fuellogproject.updateAnddelete.updateLog(updateAnddelete.java:499) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at com.example.fuellogproject.updateAnddelete.onClick(updateAnddelete.java:411) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.view.View.performClick(View.java:4261) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.view.View$PerformClick.run(View.java:17356) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.os.Handler.handleCallback(Handler.java:615) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.os.Looper.loop(Looper.java:137) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at android.app.ActivityThread.main(ActivityThread.java:4921) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at java.lang.reflect.Method.invokeNative(Native Method) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at java.lang.reflect.Method.invoke(Method.java:511) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
01-27 01:13:39.676: E/AndroidRuntime(29370): at dalvik.system.NativeStart.main(Native Method) 

這是我的代碼。

private void updateLog(fuelLogPojo updatefuelLogPojo) { 
      // TODO Auto-generated method stub 
      AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this); 
      SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase(); 


      // ContentValues class is used to store a set of values 
      //It is like name-value pairs 
      // "value" part contains the values that we are going to UPDATE 
      ContentValues contentValues = new ContentValues(); 
      contentValues.put(AndroidOpenDbHelper.KEY_DATE, dateEditTextValue); 
      contentValues.put(AndroidOpenDbHelper.KEY_PRICE, priceEditTextValue); 
      contentValues.put(AndroidOpenDbHelper.KEY_FUEL, pumpEditTextValue); 
      contentValues.put(AndroidOpenDbHelper.KEY_COST, costTextViewValue); 
      contentValues.put(AndroidOpenDbHelper.KEY_ODM, odometerEditTextValue); 
      contentValues.put(AndroidOpenDbHelper.KEY_PREODM, preodometerEditTextValue); 
      contentValues.put(AndroidOpenDbHelper.KEY_CON, fconTextViewValue); 

      // If we are using multiple whereClauseArguments, array size should have to change 
      String[] whereClauseArgument = new String[1]; 
      whereClauseArgument[0] = updatefuelLogPojo.getdate(); 

      System.out.println("whereClauseArgument[0] is :" + whereClauseArgument[0]); 

      /** 
      * This is the normal SQL query for UPDATE 
      UPDATE table_name 
      SET column1=value, column2=value2,... 
      WHERE some_column=some_value 
      */  

      sqliteDatabase.update(AndroidOpenDbHelper.TABLE_NAME_LOG, contentValues, BaseColumns._ID+"=? AND date+=?", whereClauseArgument); 
      // For two whereClauseArguments 
      //sqliteDatabase.update(AndroidOpenDbHelper.TABLE_NAME_GPA, contentValues, BaseColumns._ID+"=? AND name=?", whereClauseArgument); 

      sqliteDatabase.close(); 
      finish(); 
     }  
+0

你能解釋一下嗎?你怎麼知道他們沒有更新?你記錄什麼,看看新記錄是什麼?你有任何錯誤? – codeMagic

+0

@codeMagic我沒有登錄,沒有錯誤 – Chloe

+0

好吧,再次,「你怎麼知道他們沒有更新?」。 – codeMagic

回答

0

whereClauseArgument是一個String數組,但在這裏你有任何索引! sqliteDatabase.delete(AndroidOpenDbHelper.TABLE_NAME_LOG,BaseColumns._ID +「=?」,whereClauseArgument);

我不確定這個問題。但看看。 可能你必須這樣寫: sqliteDatabase.delete(AndroidOpenDbHelper.TABLE_NAME_LOG,BaseColumns._ID +「=」,whereClauseArgument [0]);

0
... WHERE _id=? AND date+=? 

似乎還有一個額外的+導致語法錯誤。

相關問題