gpt4 book ai didi

java - Android - sqlite in 子句使用数组列表中的字符串值?

转载 作者:太空宇宙 更新时间:2023-11-04 09:11:08 25 4
gpt4 key购买 nike

public void DBSearchCategory(String tableName) {
// 1st way
String inClause = s1.ListViewCategory.toString();
inClause = inClause.replace("[", "(");
inClause = inClause.replace("]", ")");

// Cursor cursor = database.rawQuery("SELECT CATEGORY FROM " + tableName
// + " WHERE CATEGORY NOT IN " + inClause
// + " ORDER BY RANDOM() LIMIT 1 ", null);

// 2nd way
try {
StringBuilder sb = new StringBuilder("");
for (String param : s1.ListViewCategory) {
sb.append(",").append('"').append(param).append('"');
}
params = sb.toString().substring(1);
Log.v("Tag", "params value is " + params);
} catch (StringIndexOutOfBoundsException e) {

}
Cursor cursor = database.rawQuery("SELECT CATEGORY FROM " + tableName
+ " WHERE CATEGORY NOT IN (?) "
+ " ORDER BY RANDOM() LIMIT 1 ", new String[]{params});

while (cursor.moveToNext()) {
category = cursor.getString(cursor.getColumnIndex("CATEGORY"));
s1.keyCategory = category;
}
cursor.close();
}
  1. s1.ListViewCategory是Singleton类s1中的String类型ArrayList,它的类别值有:“游戏”,“国家”,“城市”,“地铁”,“ Actor ”,“宠物”//数据库中总共有33个类别,我想排除s1.ListViewCategory中的这6个类别

  2. 在 rawQuery 中,我想排除 s1.ListViewCategory 中的类别,因此我尝试了两种光标方式来引用这 2 个 stackoverflow 问题: Android - sqlite in clause using string values from array?///Android - sqlite in clause using values from array

我使用 WHERE 和 NOT IN 语句排除这 6 个类别

  • 当我尝试第二向光标时,没有出现错误。但是,Sql查询不起作用。它必须排除 String[params] 中的类别,但它不起作用。所以我使用 log 来查看参数是什么,然后我得到了这个

    2020-01-09 09:16:47.233 8978-8978/com.kj.word V/Tag:参数值为 “游戏”、“国家”、“城市”、“地铁”、“ Actor ”、“宠物”

  • 当我尝试第一个光标类别时,我收到错误 logcat:

    Error Code : 1 (SQLITE_ERROR)
    Caused By : SQL(query) error or missing database.
    (no such column: game (code 1): , while compiling: SELECT CATEGORY FROM KeyWordDB WHERE CATEGORY
    NOT IN (game, country, city, subway, actor, pet) ORDER BY RANDOM() LIMIT 1)
    #################################################################
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1008)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:573)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
    at android.database.sqlite.SQLite
  • 我确认有数据库,所以我猜测可能是sql查询问题...

  • 所以我的问题是如何修复第一个或第二个光标以排除 s1.ListViewCateogry 中的类别?
  • 我很努力地搜索,但找不到答案...如果有人回答这个问题,我将非常感激

    最佳答案

    将构造逗号分隔列表的循环内的双引号更改为单引号:

    for (String param : s1.ListViewCategory) {
    sb.append(",").append("'").append(param).append("'");
    }
    params = sb.toString().substring(1);

    此代码构造一个列表,如下所示:

    'game', 'country', 'city', 'subway', 'actor', 'pet'

    如果您将其用作 rawQuery() 方法中的参数,则此列表将被视为字符串文字,而不是值列表。
    因此,请这样做:

    String sql = "SELECT CATEGORY FROM " + tableName
    + " WHERE CATEGORY NOT IN (?) "
    + " ORDER BY RANDOM() LIMIT 1 ";
    sql = sql.replace("?", params);
    Cursor cursor = database.rawQuery(sql, null);

    请注意,此方法容易出现sql注入(inject)。

    另一种方法是创建 ? 占位符列表而不是 1 个占位符,并将值列表作为字符串数组传递,如下所示:

    for (String param : s1.ListViewCategory) {
    sb.append(",?");
    }
    String[] array = ListViewCategory.toArray(new String[s1.ListViewCategory.size()]);

    params = sb.toString().substring(1);
    String sql = "SELECT CATEGORY FROM " + tableName
    + " WHERE CATEGORY NOT IN (@) "
    + " ORDER BY RANDOM() LIMIT 1 ";
    sql = sql.replace("@", params);

    Cursor cursor = database.rawQuery(sql, array);

    关于java - Android - sqlite in 子句使用数组列表中的字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59655721/

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