问题描述
我有条件为currency in ?
的SQL查询我正在使用vertx JDBC客户端queryWithparams
方法,该方法在JsonArray中接收查询参数.
I have SQL query with condition currency in ?
and I'm using vertx JDBC client queryWithparams
method, which receives query parameters in JsonArray.
如何将可能的currency
值列表传递给查询?我尝试了new JsonArray().add(new JsonArray(currencies)
但出现了异常
How can I pass my list of possible currency
values to the query?I tried new JsonArray().add(new JsonArray(currencies)
but got exception
推荐答案
简短的答案是,您无法使用通用的Vertx JDBC客户端添加列表作为查询参数,但是由于您使用的是Postgres,您可以使用特定于Postgres的称为vertx-pg-client的库.我实现了与您对此代码大致相同的查询:
The short answer is that you can't add a List as a query parameter with the generic Vertx JDBC client, but since you're using Postgres there is a Postgres-specific library called vertx-pg-client that you can use. I implemented roughly the same query as you did with this code:
List<String> currencies = whatever();
String uri = "your-uri";
String query = "select from table where currency = any($1)";
PgConnection.connect(vertx, uri, connectionResult -> {
if (connectionResult.failed()) {
// handle
} else {
PgConnection connection = connectionResult.result();
Tuple params = Tuple.of(currencies);
doQuery(query, connection, params).setHandler(queryResult -> {
connection.close();
msg.reply(queryResult.result());
});
}
});
private Future<String> doQuery(String sql, PgConnection connection, Tuple params) {
Promise<String> promise = Promise.promise();
connection.preparedQuery(sql, params, res -> {
if (res.failed()) {
// log
promise.fail(res.cause());
} else {
RowSet<Row> rowSet = res.result();
// do something with the rows and construct a return object (here, a string)
String result = something;
promise.complete(result);
}
});
return promise.future();
}
所有荣誉归功于@tsegismont,他曾帮助我解决相同的问题.
All credit goes to @tsegismont who helped me with the same question here.
这篇关于Vertx JDBC客户端queryWithParams-如何添加列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!