python之executemany使用

今天用python连接mysql数据库,在插入多条数据的时候,有个语法一直不会用,就是executeman,args参数的格式其实是一个元祖,其他格式都不行

具体代码如下:

def insertMore(self,condition,params):

      try:

            self.cur.executemany(condition,params)

            self.conn.commit()

            return True

        exceptpymysql.Errorase:

            print("Mysql Error %d:%s"%(e.args[0],e.args[1]))

            logging.basicConfig(filename=os.path.join(os.getcwd(),'./log.txt'),

            level= logging.DEBUG,

           format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')

            logger = logging.getLogger(__name__)

            logger.exception(e)

            return  False

if__name__=="__main__":

        test = OperationDb_interface()#实例化类

        错误用法

        #result = test.insertMore('INSERT INTO student values(8,"aaa"),param=xxx')

        正确用法

        result = test.insertMore('INSERT INTO student values(%s,%s)',params=[(9,"ab"),(6,"abcd"),(7,"abcde")])

        print(result)

你可能感兴趣的:(python之executemany使用)