版本信息:python:3.6 mysql:5.7 pyMysql:0.7.11
################################################################# #author: 陈月白 #_blogs: http://mit() #确认提交 print("INFO:execute sql sucess. sqlLine = ", sqlLine) except Exception as e: print("ERROR:execute sql failed.errorInfo =",e) print("ERROR:FUNCTION executeSql execute failed.sqlLine =",sqlLine) connect.rollback() #回滚db return str(e) + " sqlLine = " + sqlLine #断开连接 connect.close() print("INFO:connect closed.\n") return executeResult else: print("ERROR:param sqlLine is empty or type is not str.sqlLine = ",sqlLine) def executeBatchSql(self,sqlList): """ 批量执行sql exp: executeBatchSql([sql_1, sql_2, sql_3, ...... ]) """ finalResultList = [] if sqlList: for sql in sqlList: executeResult = self.executeSqlLine(sql) finalResultList.append(executeResult) else: print("ERROR:param sqlList is empty.") return finalResultList测试代码:
# -*- coding: utf-8 -*-from my_code.work_tools import WorkToolsmysql = WorkTools.MysqlTools("localhost","testdbname","rootuername","passwd")#执行单行sqlret1 = mysql.executeSqlLine("show databases")#批量执行ret2 = mysql.executeBatchSql([ "show databases", "show tables", "update students_info set name = '王大花D' where id = 2", "select * from students_info", "error sql test" #异常sql测试 ])print("ret1 = ",ret1)print("---------------------")for i in ret2: print(i)测试表:
执行结果:
以上这篇python 3.6 +pyMysql 操作mysql数据库(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。