【发布时间】:2020-09-21 20:53:05
【问题描述】:
我正在使用 mysql 连接器 python 连接到 mysql 并通过设置 multi=True 运行多个查询。我得到了结果。如果任何语句中存在 sql 错误,则忽略错误之后的查询。即使发生 SQL 错误,如何使用“--force”并继续
conn = MYSQL.MySQLConnection(user=sql_username,password=sql_password,host='127.0.0.1',database=sql_main_database,port=3306)
cursor = conn.cursor(buffered=True)
try:
results=cursor.execute("select now();SELECT UNIX_TIMESTAMP(now());select 'test';SELECT UTC_TIMESTAMP();select 'aravinth';select yuu;show processlist;select 1",multi=True)
except Exception as e:
print(e)
count = 1
for result in results:
if result.with_rows:
print("Rows produced by statement '{}':".format(
result.statement))
print(result.fetchall())
else:
print("Number of rows affected by statement '{}': {}".format(
result.statement, result.rowcount))
except Exception as e:
print(e)
我的输出是
Rows produced by statement 'select now()':
[(datetime.datetime(2020, 6, 3, 13, 4, 54),)]
Rows produced by statement 'SELECT UNIX_TIMESTAMP(now())':
[(1591169694,)]
Rows produced by statement 'select 'test'':
[('test',)]
Rows produced by statement 'SELECT UTC_TIMESTAMP()':
[(datetime.datetime(2020, 6, 3, 7, 34, 54),)]
Rows produced by statement 'select 'aravinth'':
[('aravinth',)]
1054 (42S22): Unknown column 'yuu' in 'field list'
end
即使任何中间查询失败,我也想继续执行所有查询。
【问题讨论】:
标签: python mysql python-3.x