Cassandra ODBC参数绑定

时间:2015-11-13 09:20:28

标签: cassandra odbc

我已经安装了DataStax Community Edition,并添加了DataStax ODBC连接器。现在我尝试通过pyodbc访问数据库:

import pyodbc
connection = pyodbc.connect('Driver=DataStax Cassandra ODBC Driver;Host=127.0.0.1',
                            autocommit = True)
cursor = connection.cursor()
cursor.execute('CREATE TABLE Test (id INT PRIMARY KEY)')
cursor.execute('INSERT INTO Test (id) VALUES (1)')
for row in cursor.execute('SELECT * FROM Test'):
    print row

工作正常并返回

>>> (1, )

然而,当我尝试

cursor.execute('INSERT INTO Test (id) VALUES (:id)', {'id': 2})

我得到了

>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 11, in <module>
    cursor.execute('INSERT INTO Test (id) VALUES (:id)', {'id': 2})
pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000')

替代品不起作用:

cursor.execute('INSERT INTO Test (id) VALUES (:1)', (2))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 11, in <module>
    cursor.execute('INSERT INTO Test (id) VALUES (?)', (2))
pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000')

cursor.execute('INSERT INTO Test (id) VALUES (?)', (2))

>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pyodbc.Error: ('HY000', "[HY000] [DataStax][CassandraODBC] (15) Error while preparing a query in Cassandra: [33562624] : line 1:31 no viable alternative at input '1' (...Test (id) VALUES (:[1]...) (15) (SQLPrepare)")

我的Cassandra版本是2.2.3,ODBC驱动程序来自DCG notation

1 个答案:

答案 0 :(得分:0)

根据pyodbc文档,您的查询应该是

cursor.execute('INSERT INTO Test (id) VALUES (?)', 2)

有关pyodbc Insert

的更多详情

根据评论有一个帖子说它是pyodbc中的一个开放的bug BUG