Sai*_*f S 6 python python-3.x pymysql
使用最新版本的 PyMySQL(0.9.3) 我无法执行一个查询字符串,其中包含多个用分号 (;) 分隔的 SQL 语句
我的python版本是:Python 3.6.5
import pymysql # version 0.9.3
conn = {
"host": "mysql_server_hostname",
"password": "my_password",
"port": <<port_no>>,
"user": "my_username"
}
my_query_str = 'show databases;use random_existing_db;'
with pymysql.connect(**conn):
cur.execute(query)
Run Code Online (Sandbox Code Playgroud)
返回错误:(1064,'在单个查询中检测到多个语句)
尝试使用 PyMySQL-0.7.11 运行相同的查询并且它有效
Sai*_*f S 12
在 pymysql 0.8 版以后,默认不再设置客户端标志。要执行多个语句,需要传递 client_flag=CLIENT.MULTI_STATEMENTS。
参考:https : //github.com/PyMySQL/PyMySQL/blob/v0.9.3/CHANGELOG#L66
下面的修复示例。
import pymysql
from pymysql.constants import CLIENT
conn = {
"host": "mysql_server_hostname",
"password": "my_password",
"port": <<port_no>>,
"user": "my_username",
"client_flag": CLIENT.MULTI_STATEMENTS
}
my_query_str = 'show databases;use random_existing_db;'
with pymysql.connect(**conn) as cur:
cur.execute(my_query_str)
Run Code Online (Sandbox Code Playgroud)