【发布时间】:2015-07-09 06:21:03
【问题描述】:
我有一个包含所有 mysql 查询的 .sql 脚本。现在假设在执行脚本后我想检查查询是否成功运行。如果我们这样做,在命令行中
echo $?
如果查询成功则返回0,不成功则返回1。
不,我想用 python 脚本来做。如何才能做到这一点 ?
我的python脚本:
import mysql.connector
from mysql.connector import errorcode
try:
cnx = mysql.connector.connect(user='myuser', password='mypass', host='127.1.2.3', database='mydb')
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cursor = cnx.cursor()
cursor.executescript("sql_queries.sql")
cnx.close()
【问题讨论】: