gpt4 book ai didi

SQLiteQueryBuilder.buildQuery 不使用 selectArgs?

转载 作者:搜寻专家 更新时间:2023-10-30 21:40:23 24 4
gpt4 key购买 nike

好的,我正在尝试查询 sqlite 数据库。我试图做好并使用 SQLiteDatabase 的查询方法并传入 selectArgs 参数中的值以确保所有内容都已正确转义,但它不起作用。我从未返回任何行(也没有错误)。

我开始对它生成的 SQL 感到好奇,所以我又四处寻找并找到了 SQLiteQueryBuilder (显然 Stack Overflow 不能很好地处理带有括号的链接,所以我无法链接到 buildQuery 方法的 anchor ),我假设它使用相同的逻辑来生成 SQL 语句。我这样做了:

  SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(BarcodeDb.Barcodes.TABLE_NAME);

String sql = builder.buildQuery(new String[] { BarcodeDb.Barcodes.ID, BarcodeDb.Barcodes.TIMESTAMP, BarcodeDb.Barcodes.TYPE, BarcodeDb.Barcodes.VALUE },
"? = '?' AND ? = '?'",
new String[] { BarcodeDb.Barcodes.VALUE, barcode.getValue(), BarcodeDb.Barcodes.TYPE, barcode.getType()},
null, null, null, null);

Log.d(tag, "Query is: " + sql);

此时记录的 SQL 是:

SELECT _id, timestamp, type, value FROM barcodes WHERE (? = '?' AND ? = '?')

但是,下面是 SQLiteQueryBuilder.buildQuery 的文档中关于 selectAgs 参数的内容:

You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection.

...但它不起作用。有什么想法吗?

最佳答案

SQLiteQueryBuilder.buildQuery 的文档还说,“这些值将被绑定(bind)为字符串。”这告诉我它在做直截了当的事情,即编写 SQL 离开 ?参数标记就位,这就是您所看到的,并将 selectArgs 绑定(bind)为输入参数。

那个?在运行查询时被 sqlite 替换,而不是在 SQL 字符串中。当查询实际执行时,数组中的第一个字符串将出现在您看到第一个 ? 的位置,依此类推。我希望记录的 SQL 仍然有 ?标记。

您的查询可能会失败,因为您引用了 ?。例如,不要使用 WHERE ID = '?',只需使用 WHERE ID = ?,并确保 selectArgs 是满足查询的字符串。

关于SQLiteQueryBuilder.buildQuery 不使用 selectArgs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2481322/

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