SQLite异常:删除查询

时间:2016-01-06 12:53:43

标签: java android sqlite

我的大学项目存在问题。我试图使用以下查询从我的一个表中删除一行。

//---deletes a particular match---
public boolean deleteMatch(String name) {
    SQLiteDatabase sqLiteDatabase = getWritableDatabase();
    return sqLiteDatabase.delete(TABLE_FIXTURES, MATCH_OPPONENT + " = " + name, null) > 0;
}

这里我尝试删除基于match_opponent的记录并传递字符串值 name

然后我在下面的EditSchedule活动中调用此方法:

public class EditSchedule extends AppCompatActivity {

    public Button fixtureSearch;
    public EditText opponentName;
    public String searchTerm;

    ListView editMatch;

    DBHelper dbHelper = new DBHelper(this);

    SQLiteDatabase sqLiteDatabase;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editfixture);

        editMatch = (ListView) findViewById(R.id.listViewEditMatch);
        opponentName = (EditText) findViewById(R.id.fixtureOpponentDelete);
        searchTerm = opponentName.getText().toString();

        fixtureSearch = (Button) findViewById(R.id.fixtureSearchButton);
        fixtureSearch.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dbHelper.deleteMatch(searchTerm);
                    }
                }
        );
    }
}

因此,当我通过在夹具表中键入对手的名称并单击删除按钮来测试我的应用程序时,我收到以下错误:

01-06 07:49:42.476  25778-25778/com.example.myacer.clubhub E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: DELETE FROM fixtures WHERE match_opponent =
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
            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.delete(SQLiteDatabase.java:1494)
            at com.example.myacer.clubhub.database.DBHelper.deleteMatch(DBHelper.java:243)
            at com.example.myacer.clubhub.manager.EditSchedule$1.onClick(EditSchedule.java:47)
            at android.view.View.performClick(View.java:4240)
            at android.view.View$PerformClick.run(View.java:17721)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

谁能看到我哪里出错了?所有人都非常感谢。

2 个答案:

答案 0 :(得分:3)

您在deleteMatch()中传递空白值,将searchTerm = opponentName.getText().toString();放在onClick()

fixtureSearch.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    searchTerm = opponentName.getText().toString();
                    dbHelper.deleteMatch(searchTerm);
                }
                }
    );

同时更改deleteMatch()方法

sqLiteDatabase.delete(TABLE_FIXTURES, MATCH_OPPONENT + " = ?",new String[]{name});

答案 1 :(得分:2)

试试这个:

sqLiteDatabase.delete(TABLE_FIXTURES, MATCH_OPPONENT + " = ?", new String[]{name})