import pymysql #连接数据库 def getConnect(): conn = pymysql.connect( host='1.1.1.1',#数据库ip port=3306,#数据库port user='root',#数据库名称 passwd='root',#数据库密码 db='test',#数据库名称 charset='utf8') return conn def executeScriptsFromFile(filename,cur): #fd = open(filename,'r',encoding='utf-8') #sqlFile = fd.read() #fd.close() #为什么使用with open 可参考https://blog.csdn.net/xinyuski/article/details/88865162 with open(filename,'r',encoding='utf-8') as fd:#fd文件句柄 sqlFile = fd.read() sqlCommands = sqlFile.split('\n') for command in sqlCommands: try: cur.execute(command) except Exception as msg: print(msg) print('sql执行完成') conn = getConnect() # 生成游标对象 cur = conn
python读取sql文件,批量操作数据库
最新推荐文章于 2024-05-01 16:30:00 发布