我想通过它更新/删除项目。
当我尝试我的代码时, 在我选择“更新按钮”后,没有任何内容正在更新。
当我尝试“删除按钮”时,也没有删除任何内容。
我想根据它删除我的商品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 :(得分:0)
whereClauseArgument是一个String数组,但在这里你有任何索引! sqliteDatabase.delete(AndroidOpenDbHelper.TABLE_NAME_LOG,BaseColumns._ID +“=?”,whereClauseArgument);
我不确定这个问题。但看看。 可能你必须这样写: sqliteDatabase.delete(AndroidOpenDbHelper.TABLE_NAME_LOG,BaseColumns._ID +“=”,whereClauseArgument [0]);
答案 1 :(得分:0)
... WHERE _id=? AND date+=?
似乎有一个额外的+
导致语法错误。