Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

debashish47_150403 avatar image
debashish47_150403 asked debashish47_150403 commented

How to bind parameters in the IN clause of a prepared statement?

I'm using cpp-driver for cassandra and I want to use prepared statements. Binding parameters is easy except when I'm using IN clause in the query.

What is the correct way to form such query in this case? Below are some examples:

  • select * from some_table where pkey = ? AND ckey1 IN (:c1values) AND ckey2 IN (:c2values);
  • select * from some_table where pkey = ? AND ckey1 IN :c1values AND ckey2 IN :c2values;

Also, how to bind parameters to the markers?

cpp driver
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Cedrick Lunven avatar image
Cedrick Lunven answered debashish47_150403 commented

Hi @debashish47_150403 and thank for your questions.


I am more specialised on the Java drivers but I am pretty sure that :

- you should remove the parenthesis in you syntax.

- mixing both labeled parameters and position parameter is not what you should do.


I would propose :

select * from some_table where pkey = ? AND ckey1 IN ? AND ckey2 IN ?

You will provide List or Arrays for params 2 and 3 and you should be good to go.

Regards.

3 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Thank you Cedrick for clearing my first doubt.

I have used python driver before and it seems pretty clear to use similar data structures such as lists or arrays. Infact, it works with mixed style of markers too.

However, the C/C++ API is not so clear and I can't seem to find the correct way to bind parameters to an "IN clause" marker.

0 Likes 0 ·
smadhavan avatar image smadhavan ♦ debashish47_150403 ·

@debashish47_150403, in addition to the above answer, please see the below references (though these doesn't specially cover the IN clause):

1 Like 1 ·

I had already gone thorough these resources. I found the correct way finally. I used Collections API with list data type.

It makes sense since IN clause contain lists of same typed data. So the contenders were lists and set. I tried set but it didn't work. Lists are the way to go.

0 Likes 0 ·