簡體   English   中英

嘗試使用mysql python連接器執行預准備語句時,我得到NotImplementedError

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

我想使用預處理語句使用python將數據插入MySQL數據庫(版本5.7),但我一直得到一個NotImplementedError。 我在這里關注文檔: https//dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursorprepared.html

使用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

這是我正在運行的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

如果您擁有 CEXT,默認啟用 CEXT ,並且在撰寫本文時,CEXT不支持預備語句

您可以通過添加關鍵字參數use_pure=True來禁用連接CEXT,如下所示:

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

CEXT中對mysql-connector-python准備語句的支持將包含在即將發布的mysql-connector-python 8.0.17版本中(根據MySQL錯誤報告 )。 因此,一旦可用,升級到至少8.0.17以解決此問題,而無需use_pure=True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM