我想使用python使用prepared语句将数据插入mysql数据库(版本5.7),但我一直得到一个notimplementederror。下面是我的文档:https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursorprepared.html
使用python 2.7和mysql connector python库的8.0.11版本:
pip show mysql-connector-python
---
Metadata-Version: 2.1
Name: mysql-connector-python
Version: 8.0.11
Summary: MySQL driver written in Python
Home-page: http://dev.mysql.com/doc/connector-python/en/index.html
这是我正在运行的python脚本的清理版本(没有特定的主机名、用户名、密码、列或表):
import mysql.connector
from mysql.connector.cursor import MySQLCursorPrepared
connection = mysql.connector.connect(user=username, password=password,
host='sql_server_host',
database='dbname')
print('Connected! getting cursor')
cursor = connection.cursor(cursor_class=MySQLCursorPrepared)
select = "SELECT * FROM table_name WHERE column1 = ?"
param = 'param1'
print('Executing statement')
cursor.execute(select, (param,))
rows = cursor.fetchall()
for row in rows:
value = row.column1
print('value: '+ value)
运行此命令时出现以下错误:
Traceback (most recent call last):
File "test.py", line 18, in <module>
cursor.execute(select, (param,))
File "/home/user/.local/lib/python2.7/site-packages/mysql/connector/cursor.py", line 1186, in execute
self._prepared = self._connection.cmd_stmt_prepare(operation)
File "/home/user/.local/lib/python2.7/site-packages/mysql/connector/abstracts.py", line 969, in cmd_stmt_prepare
raise NotImplementedError
NotImplementedError
1条答案
按热度按时间fdx2calv1#
如果您有cext,它将在默认情况下启用,并且在编写本文时cext不支持prepared语句。
通过添加关键字参数,可以在连接时禁用cext
use_pure=True
具体如下:对cext中准备好的语句的支持将包括在即将到来的报告中
mysql-connector-python
8.0.17版本(根据mysql错误报告)。因此,一旦可用,就至少升级到8.0.17以解决此问题,而无需use_pure=True
.