如何在 CQL 3 准备好的语句中绑定 IN 子句值? [英] How to bind IN-clause values in a CQL 3 prepared statement?

查看:24
本文介绍了如何在 CQL 3 准备好的语句中绑定 IN 子句值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张大致像的桌子

create table mytable (
   id uuid,
   something text,
   primary key (id)
);

我正在尝试创建一个带有绑定子句的准备好的语句:

I'm trying to create a prepared statement that has a bound in-clause:

PreparedStatement ps = session.prepare("select * from mytable where id IN (?)");
...
UUID[] ids = { uuid1, uuid2, uuid3} ;

无论我如何表达要绑定的 id,java 驱动程序都会拒绝它们.

No matter how I express the ids to bind, the java driver rejects them.

ps.bind(/*as array*/):驱动程序抱怨语句只有一个值,提供了 2 个

ps.bind( /*as array*/): driver complains statement has only one value, 2 supplied

ps.bind(/*as 逗号分隔的 uuids 字符串列表*/):驱动程序抱怨它想要 UUID.class 对象,而不是字符串

ps.bind( /*as comma separated string list of uuids*/): driver complains it wants UUID.class objects, not strings

ps.bind(/*as list object*/):驱动程序抱怨它想要 UUID.class 对象,而不是 List.class 对象

ps.bind( /*as list object*/): driver complains it wants UUID.class objs, not a List.class object

我真的希望司机不要期待那么多?占位符,因为 in-list 中有值,因为这意味着每次要执行该语句时都必须重新准备该语句,Datastax 文档说不要这样做!

I really hope the driver isn't expecting as many ? place holders as there are values in the in-list, because that means you'd have to reprepare the statement every time you wanted to execute it, which the Datastax docs says not to do!

我查看了 com.datastax.driver.core.BoundStatement.bind() 方法,没有迹象表明其他任何方法都可以工作 - 没有魔法包装器或任何东西.

I looked at the com.datastax.driver.core.BoundStatement.bind() method and there is no indication that anything else would work - no magic wrappers or anything.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

正确的语法是 SELECT * FROM mytable WHERE id IN ?(? 周围没有括号).这将允许您传递 UUID 列表以绑定到该单个查询参数.

The correct syntax is SELECT * FROM mytable WHERE id IN ? (without parens around the ?). This will allow you to pass a list of UUIDs to bind to that single query parameter.

这篇关于如何在 CQL 3 准备好的语句中绑定 IN 子句值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆