Chameleon
Chameleon

Reputation: 10128

How can I bind dict to Prepared Cassandra Statement in Python?

How can I pass dict and query parameters?

I can do such code.

prepared = session.prepare('select name from task where id = ?;')
bound = prepared.bind([1])
session.execute(bound)

How can I use dict as parameters and what will be query syntax?

This not works:

prepared = session.prepare('select name from task where id = %(id)s;')
bound = prepared.bind({"id": 1})
session.execute(bound)

Can you help with this basic code - it looks that it is possible but I do not know valid query syntax?

Upvotes: 2

Views: 2962

Answers (1)

Rafał Łużyński
Rafał Łużyński

Reputation: 7312

query = """
        INSERT INTO table_name (
            field_1,
            field_2
        ) VALUES (?, ?)
"""
cql_session.prepare(query).bind({'field_1': 'foo', 'field_2': 'bar'})

this works for us.

Docs says that

bind(values)

Binds a sequence of values for the prepared statement parameters and returns this instance. Note that values must be:

  • a sequence, even if you are only binding one value, or
  • a dict that relates 1-to-1 between dict keys and columns

Upvotes: 5

Related Questions