【发布时间】:2015-11-13 18:04:25
【问题描述】:
我已经安装了 DataStax 社区版,并添加了 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 驱动来自https://downloads.datastax.com/odbc-cql/1.0.1.1002/
【问题讨论】:
-
使用 ODBC 和 pyodbc 代替本机驱动程序的任何原因?
-
其实我想从 Delphi 访问 Cassandra,而 ODBC 几乎是我唯一的选择。我在这里使用了 Python 脚本,以便更多人可以理解代码。