gpt4 book ai didi

java - 用户 t1, t2, 的 SQL 查询。 。 。 , tm where 子句中的术语

转载 作者:行者123 更新时间:2023-12-01 19:43:57 29 4
gpt4 key购买 nike

用户提供字符串作为术语的输入,它们可以是 t1, ...tm 现在我必须将这些 t1,...tm 嵌入到 sql where 子句中。从 term = t1 OR term = t2 ...... term=tm

的文档中选择 *

目前我正在将字符串拆分为字符串数组:

String[] terms = term.split("\\s+");     
for (int i =0; i<term.length; i++) {
if (i == term.length -1) {
str += "term = " + term[i];
}
else {
str += "term = " + term[i] + " OR ";
}

现在我得到

string str= "term = document OR term = word Or term = explanation".

但是 term 是我的列名和文档值,我如何将其传递到 SQL 的 where 子句中?

最佳答案

我假设,由于您是按空格分隔的,所以用户的输入如下所示:

document word explanation

首先使用 trim() 删除 term 中的所有前导空格。
然后在 for 循环内将数组的所有项目用单引号括起来(尽管这不是构造查询的安全方法,但您可以使用准备好的语句和 ?占位符):

String[] terms = term.trim().split("\\s+");
for (int i = 0; i < terms.length; i++) {
str += "term = '" + terms[i] + "'";
if (i < terms.length -1) {
str += " OR ";
}
}

结果将是:

term = 'document' OR term = 'word' OR term = 'explanation'

关于java - 用户 t1, t2, 的 SQL 查询。 。 。 , tm where 子句中的术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59147113/

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