• python mysql插入多条数据


    #!/usr/bin/env python  
    # encoding: utf-8  
    import pymysql
    from config.config import *
    import datetime
    def get_conn():
       conn=pymysql.connect(host=HOST,port=PORT,user=USER,passwd=PASSWORD,db=DB,charset=CHARSET, cursorclass=pymysql.cursors.DictCursor)
       return conn
    def get_idlist():
        datalist=[]
        conn=get_conn()
        try:
            with conn.cursor() as cursor:
                    sql="select PAGE_CD,PAGE_NAME from sns_page where PAGE_ID in(%s,%s,%s)"%tuple(IDLIST)
                    print(sql)
                    cursor.execute(sql)
                    datalist=cursor.fetchall()
        except Exception as e:
            print(e.args)
        finally:
            conn.close()
            return datalist
    def save_data(dicts):
            now=datetime.datetime.now()
            conn=get_conn()
            data=((now,dicts["PAGE_CD"],"FB","FANS_NUM",dicts["FANS_NUM"],now),(now,dicts["PAGE_CD"],"FB","FOLLOW_NUM",dicts["FOLLOW_NUM"],now))
            try:
                with conn.cursor() as cursor:
                    sql="insert into sns_insight_log(LOG_DATE,BIZ_ID,MEDIA_TYPE,ITEM_NAME,DEC_VALUE,UPD_DATE) values(%s,%s,%s,%s,%s,%s)"
                    cursor.executemany(sql,data)
                    conn.commit()
            except:
                conn.rollback()
            finally:
                conn.close()
    if __name__=="__main__":
        print(get_idlist())
    

      

  • 相关阅读:
    文章标题
    PAT Basic Level 1006
    PAT Basic Level 1005
    PAT Basic Level 1004
    PAT Basic Level 1036
    PAT Basic Level 1002
    (算法)递归各习题总结反思 *基础
    uvaoj 101
    C语言(关于二级指针)
    普通算法(高效幂运算)
  • 原文地址:https://www.cnblogs.com/c-x-a/p/9068128.html
Copyright © 2020-2023  润新知