标签:object class 数据 odi t_sql commit htm %s exe
多条插入用excutemany(listtuple)
#coding=utf-8 import MySQLdb import traceback sqlstr= "insert into table1 values(%s,%s,%s,%s);" #SQL模板字符串 如果懒的写每一个属性对应的字段,那么后面的s%的个数要和数据库表中属性个数一样(所有类型,都用%s表示) lists=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
dataList= [tuple(x) for x in lists] # 参数是一个个元组,list里嵌套tuple class mymysql(object): def __init__(self): self.conn = MySQLdb.connect( host=‘127.0.0.1‘, port = 3306, user = ‘root‘, passwd = ‘123456‘, db = ‘xtp3‘) def insert_sql(self,sqlstring,data): cur = self.conn.cursor() try: cur.executemany(sqlstring,data) self.conn.commit() except: self.conn.rollback() traceback.print_exc() finally: cur.close() if __name__ == ‘__main__‘: m = mymysql() m.insert_sql(sqlstr,dataList)
excute和excutemany速度对比
参考:
https://www.cnblogs.com/zeke-python-road/p/9442152.html
https://blog.csdn.net/u010339879/article/details/81623833
标签:object class 数据 odi t_sql commit htm %s exe
原文地址:https://www.cnblogs.com/51python/p/11405860.html