How to bind IN-clause values in a CQL 3 prepared statement?

旧街凉风 提交于 2019-12-04 03:53:13

问题


I have a table that is roughly like

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} ;

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

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

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

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

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!

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?


回答1:


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.



来源:https://stackoverflow.com/questions/35069238/how-to-bind-in-clause-values-in-a-cql-3-prepared-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!