尝试使用mysql python连接器执行准备好的语句时出现NotImplementedError

编程入门 行业动态 更新时间:2024-10-10 17:30:00
本文介绍了尝试使用mysql python连接器执行准备好的语句时出现NotImplementedError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用准备好的语句使用python将数据插入MySQL DB(版本5.7),但是我一直收到NotImplementedError. 我在这里关注文档: dev.mysql/doc/connector-python/zh-CN/connector-python-api-mysqlcursorprepared.html

I want to use prepared statements to insert data into a MySQL DB (version 5.7) using python, but I keep getting a NotImplementedError. I'm following the documentation here: dev.mysql/doc/connector-python/en/connector-python-api-mysqlcursorprepared.html

使用python-2.7和mysql-connector-python库的8.0.11版:

Using Python 2.7 and version 8.0.11 of mysql-connector-python library:

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: dev.mysql/doc/connector-python/en/index.html

这是我正在运行的python脚本的纯净版本(没有特定的主机名,用户名,密码,列或表):

This is a cleaned version (no specific hostname, username, password, columns, or tables) of the python script I'm running:

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

推荐答案

CEXT 将如果已启用,则默认启用,并且已准备撰写本文时,CEXT不支持这些语句.

CEXT will be enabled by default if you have it, and prepared statements are not supported in CEXT at the time of writing.

您可以在连接时通过添加关键字参数use_pure=True来禁用CEXT,如下所示:

You can disable the use of CEXT when you connect by adding the keyword argument use_pure=True as follows:

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

对CEXT中准备好的语句的支持将包含在即将发布的mysql-connector-python 8.0.17版本中(根据 MySQL错误报告).因此,一旦可用,至少升级到8.0.17即可解决此问题,而无需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). So once that is available, upgrade to at least 8.0.17 to solve this without needing use_pure=True.

更多推荐

尝试使用mysql python连接器执行准备好的语句时出现NotImplementedError

本文发布于:2023-11-10 14:26:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575622.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接器   语句   mysql   python   NotImplementedError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!