android.database.sqlite.SQLiteException:near"&引用;:语法错误(代码1):
当我试图从数据库中删除一条记录时,我遇到了这个错误。这是全部错误android.database.sqlite.SQLiteException:near"&引用;:语法错误(代码1):,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,当我试图从数据库中删除一条记录时,我遇到了这个错误。这是全部错误 FATAL EXCEPTION: main Process: itp231.dba.nyp.com.bloommain, PID: 12274
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)
您可以使用
准备好的语句
SQLiteStatement stmt = db.compileStatement("DELETE FROM events WHERE id = ?");
stmt.bindString(1, id);
stmt.execute();
您可以使用
准备好的语句
SQLiteStatement stmt = db.compileStatement("DELETE FROM events WHERE id = ?");
stmt.bindString(1, id);
stmt.execute();
1-您的id是一个空白字符串,因此无法在SQL命令中对其进行分析。
2-如果您的id字段是文本(??),则需要将其括在单引号中。
3-对于SQL命令,请使用
execSQL()
而不是rawQuery()
-rawQuery()
仅适用于。。。查询(选择
)4-以及。。。准备好的语句(或绑定参数)是更好的选择。占位符(
?
)将按其位置顺序自动替换,引号将不再是问题(Android将为您处理!)。1-您的id是一个空白字符串,因此无法在SQL命令中解析。2-如果您的id字段是文本(??),则需要将其括在单引号中。
3-对于SQL命令,请使用
execSQL()
而不是rawQuery()
-rawQuery()
仅适用于。。。查询(选择
)4-以及。。。准备好的语句(或绑定参数)是更好的选择。占位符(
?
)将按其位置顺序自动替换,引号将不再是问题(Android将为您处理!)。试试这个
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.
试试这个
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.
读取堆栈跟踪。
字符串id
为空,这意味着EditText
为空,或者只有空格。数据库中的id类型以及传递值的时间我不确定,但如果使用Android Studio,即时运行功能可能也有问题。请阅读堆栈跟踪。字符串id
为空,这意味着EditText
为空,或者只有空格。数据库中的id类型是什么,以及您传递值的时间我不确定,但如果您使用Android Studio,可能即时运行功能也有问题。@IntelliJAmiya谢谢您,亲爱的!除此之外,不要以这种方式编写SQL代码。您很容易受到SQL注入攻击。始终使用绑定变量。使用串联参数编写查询是一个可怕的错误。@IntelliJAmiya谢谢你,亲爱的!除此之外,不要以这种方式编写SQL代码。您很容易受到SQL注入攻击。始终使用绑定变量。编写带有串联参数的查询是一个可怕的错误。难道没有人听说过SQL注入并意识到为什么这段代码完全有缺陷吗?如果你没有出去学习。如果你有,那你为什么不教他正确的方法呢?没有人听说过SQL注入,也没有人意识到为什么这段代码完全有缺陷吗?如果你没有出去学习。如果你有,那你为什么不教他正确的方法呢?