美文网首页
python之executemany使用

python之executemany使用

作者: JumpSong | 来源:发表于2017-11-15 19:00 被阅读0次

今天用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使用

      本文链接:https://www.haomeiwen.com/subject/qyehvxtx.html