Update query for cassandra using variable in python -
i trying run update query variables. using casandra-driver
guided in datastax website.i using following script connect cluster , update values in table ct_table
.
from cassandra.cluster import cluster cluster = cluster() session = cluster.connect('ct_keyspace') a=90 session.execute("update ct_table set value=%d attribute='ct'",(int(a))) result = session.execute("select * ct_table attribute='ct'")[0] print result.attribute, result.value
i getting following error
traceback (most recent call last): file "connection.py", line 7, in <module> session.execute("update ct_table set value=%d attribute='ct'",(a)) file "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 1998, in execute return self.execute_async(query, parameters, trace, custom_payload, timeout, execution_profile, paging_state).result() file "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 2035, in execute_async future = self._create_response_future(query, parameters, trace, custom_payload, timeout, execution_profile, paging_state) file "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 2095, in _create_response_future query_string = bind_params(query_string, parameters, self.encoder) file "/usr/local/lib/python2.7/dist-packages/cassandra/query.py", line 823, in bind_params return query % tuple(encoder.cql_encode_all_types(v) v in params) typeerror: 'int' object not iterable
my table looks this:
cqlsh:ct_keyspace> describe ct_table create table ct_keyspace.ct_table ( attribute text primary key, value int ) bloom_filter_fp_chance = 0.01 , caching = '{"keys":"all", "rows_per_partition":"none"}' , comment = '' , compaction = {'class': 'org.apache.cassandra.db.compaction.sizetieredcompactionstrategy'} , compression = {'sstable_compression': 'org.apache.cassandra.io.compress.lz4compressor'} , dclocal_read_repair_chance = 0.1 , default_time_to_live = 0 , gc_grace_seconds = 864000 , max_index_interval = 2048 , memtable_flush_period_in_ms = 0 , min_index_interval = 128 , read_repair_chance = 0.0 , speculative_retry = '99.0percentile';
value
defined in table int
. suspect formatting string update query incorrectly.
session.execute("update ct_table set value=%d attribute='ct'",(int(a)))
should be
session.execute("update ct_table set value=%s attribute='ct'", (a,))
we're specifying bind marker %s
, passing tuple parameter list. without trailing comma, it's value.
see also: https://datastax.github.io/python-driver/getting_started.html#passing-parameters-to-cql-queries
note should use %s types of arguments, not strings
Comments
Post a Comment