如何使用MySQLdb python驱动程序执行存储在* .sql文件中的sql脚本。我正在尝试:
cursor.execute(file(PATH_TO_FILE).read())
但这不起作用,因为cursor.execute一次只能运行一个sql命令。我的sql脚本包含几个sql语句。我也在尝试:
cursor.execute('source %s'%PATH_TO_FILE)
发布于 2018-03-16 16:03:45
for line in open(PATH_TO_FILE):
cursor.execute(line)
这假定你的文件中每行有一条SQL语句。否则,你需要编写一些规则来连接在一起。
发布于 2018-03-16 17:04:36
从python开始,我启动一个mysql进程为我执行文件:
from subprocess import Popen, PIPE
process = Popen(['mysql', db, '-u', user, '-p', passwd],
stdout=PIPE, stdin=PIPE)
output = process.communicate('source ' + filename)[0]
https://stackoverflow.com/questions/-100007653
复制相似问题