gpt4 book ai didi

java - 当我想在 sqlite 表中插入一个值时,当我在 editText 中添加引号时,我的应用程序不断崩溃如何解决此问题?

转载 作者:行者123 更新时间:2023-12-01 20:22:56 24 4
gpt4 key购买 nike

请帮助我,我不知道如何修复此错误

android.database.sqlite.SQLiteException: unrecognized token: "'''" (code 1): , while compiling: select * from Repository where itemName = '''
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

这里是导致输入 Activity 和数据库hapler错误的代码

 final String query = "select * from Repository where itemName = '" + name + "'";
if (mDataBase.getData(query).getCount() > 0) {....}

以及dbhalper中的代码

public Cursor getData(String sql) {
SQLiteDatabase database = getReadableDatabase();
return database.rawQuery(sql, null);
}

最佳答案

名称似乎包含值',这需要特殊处理并且需要为'',如下所示:-

A string constant is formed by enclosing the string in single quotes ('). A single quote within the string can be encoded by putting two single quotes in a row - as in Pascal. C-style escapes using the backslash character are not supported because they are not standard SQL. expression - Literal Values (Constants)

我相信如果您使用(如果没有参数则传递 null):-

public Cursor getData(String sql, String[] args) {
SQLiteDatabase database = getReadableDatabase();
return database.rawQuery(sql, args);
}

连同:-

final String query = "select * from Repository where itemName =?";
if (mDataBase.getData(query,new String[]{name}).getCount() > 0) {....}

? 将被替换为正确的值。

关于java - 当我想在 sqlite 表中插入一个值时,当我在 editText 中添加引号时,我的应用程序不断崩溃如何解决此问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58927554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com