简体   繁体   English

尝试使用mysql python连接器执行预准备语句时,我得到NotImplementedError

[英]I get NotImplementedError when trying to do a prepared statement with mysql python connector

I want to use prepared statements to insert data into a MySQL DB (version 5.7) using python, but I keep getting a NotImplementedError. 我想使用预处理语句使用python将数据插入MySQL数据库(版本5.7),但我一直得到一个NotImplementedError。 I'm following the documentation here: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursorprepared.html 我在这里关注文档: https//dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursorprepared.html

Using Python 2.7 and version 8.0.11 of mysql-connector-python library: 使用mysql-connector-python库的Python 2.7和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

This is a cleaned version (no specific hostname, username, password, columns, or tables) of the python script I'm running: 这是我正在运行的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)

I get this error when I run this: 运行时我收到此错误:

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

CEXT will be enabled by default if you have it , and prepared statements are not supported in CEXT at the time of writing . 如果您拥有 CEXT,默认启用 CEXT ,并且在撰写本文时,CEXT不支持预备语句

You can disable the use of CEXT when you connect by adding the keyword argument use_pure=True as follows: 您可以通过添加关键字参数use_pure=True来禁用连接CEXT,如下所示:

connection = mysql.connector.connect(user=username, password=password,
                                     host='sql_server_host',
                                     database='dbname',
                                     use_pure=True)

Support for prepared statements in CEXT will be included in the upcoming mysql-connector-python 8.0.17 release (according to the MySQL bug report ). CEXT中对mysql-connector-python准备语句的支持将包含在即将发布的mysql-connector-python 8.0.17版本中(根据MySQL错误报告 )。 So once that is available, upgrade to at least 8.0.17 to solve this without needing use_pure=True . 因此,一旦可用,升级到至少8.0.17以解决此问题,而无需use_pure=True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python MySql 连接器准备好的语句和字典类 - Python MySql Connector prepared statement and dictionary class 查询许多列并使用预处理语句时,MySQL连接器/ python(v1.1.6)失败 - MySQL connector / python (v1.1.6) fails when querying for many columns and using prepared statement Python mysql连接器准备INSERT语句切断文本 - Python mysql connector prepared INSERT statement cutting off text Python MySQL,这是准备好的语句吗? - Python MySQL, is this a prepared statement? 如何在 mysql 连接器 python 查询中获得文字“%”? - How do I get a literal '%' in a mysql connector python query? 在准备好的INSERT语句中,无法在python mysql.connector中使用None(NULL)值 - Unable to use None (NULL) values in python mysql.connector in prepared INSERT statement 如何修复 Python 中的 NotImplementedError? - How do I fix the NotImplementedError in Python? 尝试在树莓派上导入 python mysql 连接器时出现异常 - Exception when trying to import python mysql connector on raspberry pi 如何成功安装 mysql 和 mysql-python 连接器? - How do I successfully install mysql and mysql-python connector? Python MySQL 连接器为准备好的语句返回字节数组而不是字符串 - Python MySQL connector returns bytearray instead of string for prepared statements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM