0

任何人都知道如何将不确定的参数传递给client.preparedQuery?请看下面的图片,感谢任何帮助!

@Override
public Future<List<User>> getByIds(List<String> ids) {
    Promise<List<User>> promise = Promise.promise();
    // How to pass the uncertain parameters to preparedQuery?
    pool.preparedQuery("SELECT * FROM users WHERE id IN ($1)", Tuple.wrap(ids.toArray()), ar -> {
        if (ar.succeeded()) {
            RowSet<Row> rows = ar.result();
            List<User> users = new ArrayList<>();
            for (Row row : rows) {
                User user = User.fromDatabaseRow(row);
                users.add(user);
            }
            promise.complete(users);
        } else {
            promise.fail(ar.cause());
        }
    });
    return promise.future();
}

在此处输入图像描述

4

1 回答 1

2

要将数组传递给 PostgreSQL,那么您必须使用= ANY(params)而不是IN(params)

SELECT * FROM users WHERE id = ANY($1)

此外,您必须将 String 数组传递给ANY,您的代码应该是:

pool.preparedQuery(
        "SELECT * FROM users WHERE id = ANY($1)", 
        Tuple.of(ids.toArray(String[]::new)), 
        ar -> {..}
)
于 2020-02-14T11:01:07.643 回答