【问题标题】:android.database.sqlite.SQLiteException: near ";": syntax error (code 1):android.database.sqlite.SQLiteException:靠近“;”:语法错误(代码 1):
【发布时间】:2016-08-05 05:31:20
【问题描述】:

我在尝试从数据库中删除记录时收到此错误。这是完整的错误

FATAL EXCEPTION: main
                                                                              Process: itp231.dba.nyp.com.bloommain, PID: 12274
                                                                              android.database.sqlite.SQLiteException: near ";": syntax error (code 1): , while compiling: DELETE FROM events WHERE id= ;
                                                                                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                                  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                                  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:1674)
                                                                                  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                                                  at itp231.dba.nyp.com.bloommain.EventInformationPage$1.onClick(EventInformationPage.java:135)
                                                                                  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

查看日志,它引导我到这行代码(我的 deleteRecord() 方法 -

private void deleteRecord() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Are you sure you want delete this person?");

    alertDialogBuilder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    String id = editTextId.getText().toString().trim();

                    String sql = "DELETE FROM events WHERE id= " + id + ";";
                    db.execSQL(sql);
                    Toast.makeText(getApplicationContext(), "Record Deleted", Toast.LENGTH_LONG).show();
                    c = db.rawQuery(SELECT_SQL,null);
                }
            });

【问题讨论】:

  • 阅读您的堆栈跟踪。 String id 是空的,这意味着 EditText 是空的,或者只有空格。
  • 什么是数据库中的 id 类型以及何时传递值
  • 我不确定,但如果您使用的是 Android Studio,可能即时运行功能也有问题。

标签: android sqlite android-sqlite


【解决方案1】:

1 - 您的 id 是一个空白字符串,因此无法在您的 SQL 命令中解析。
2 - 如果您的 id 字段是 TEXT (???),那么您需要将其括在单引号中。
3 - 对于 SQL 命令,使用 execSQL() 而不是 rawQuery() - rawQuery() 仅适用于.. . 查询 (SELECT)
4 - 并且...准备好的语句(或绑定参数)是更好的选择。占位符 (?) 将按其位置顺序自动替换,引号将不再是问题(Android 会为您处理!)。

【讨论】:

  • @IntelliJAmiya 谢谢你,亲爱的!
  • 除此之外 - 永远不要以这种方式编写 SQL 代码。您很容易受到 SQL 注入攻击。 总是使用绑定变量。使用串联参数编写查询是一个可怕的错误。
【解决方案2】:

您可以使用prepared statements

SQLiteStatement stmt = db.compileStatement("DELETE FROM events WHERE id = ?");
stmt.bindString(1, id);
stmt.execute();

【讨论】:

  • 没有人听说过 SQL 注入并意识到为什么这段代码完全有缺陷吗?如果你还没有出去学习。如果你有,那你为什么不教他正确的方法呢?
【解决方案3】:

试试这个..

 db.delete("events","id=?",new String[]{Integer.toString(id)});

其中,

first paramater -> will be table name from where the deletion of data need to de done.
Second parameter -> Selection field in table.based on which field we are going to perform the deletion
Third parameter -> specifies the value,to be compare it with second paramters field if it is matched,then the particular column will be deleted.

【讨论】:

    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多