3

I'm trying to execute a simple cql query in python and I keep getting an error.

table1 = "mytable1"
table2 = "mytable2"

query1 = "SELECT * FROM %s"
table1Rows = session.execute(query1, (table1,))
table2Rows = session.execute(query1, (table2,))

The table variables are actually passed in as arguments but I just made my own as an example. I get this error:

cassandra.protocol.SyntaxException: <ErrorMessage code=2000 [Syntax error in CQL query] message="line 1:14 no viable alternative at input 'mytable1' (SELECT * FROM ['mytable]...)">

I can't figure out what's wrong with my syntax. Please help. Thanks!

1 Answers1

3

Parametrized queries do not support providing the table name as a parameter. You can achieve it by constructing the query string via string concatenation. Just make sure that the input variable is in a whitelist of allowed values, to guard against SQL injection.

Gudmundur Orn
  • 2,003
  • 2
  • 23
  • 31