zoukankan      html  css  js  c++  java
  • excel表格用协程插入到mysql

    #!/usr/bin/python
    #coding=utf-8
    
    import MySQLdb
    import gevent
    import xlrd
    import sys
    
    
    class mysqldb:
        def __init__(self,host,username,password,db,charset='utf8'):
            self.host = host          
            #self.port = port          
            self.username = username  
            self.password = password  
            self.db = db              
            self.charset = charset    
            self.connect()
    
        def connect(self):
            self.conn = MySQLdb.connect(host=self.host,
                                        #port=self.port,
                                        user=self.username,
                                        passwd=self.password,
                                        db=self.db,
                                        charset=self.charset
                                      )
            self.asynchronous(sys.argv[1])
    
        def run(self,xxlist):
            self.cur = self.conn.cursor()
            sql = "insert into ENQD(dstip,dstloc,dstisp,srcip,srcloc,srcisp,status) VALUES (%s,%s,%s,%s,%s,%s,%s)"
            excutesqls = self.cur.executemany(sql,xxlist)
            self.conn.commit()
    
        def asynchronous(self,file):
            data_list = []
            book = xlrd.open_workbook(file)
            sheet = book.sheets()[0] 
            for r in range(1,sheet.nrows):
                dstip  =  sheet.cell(r,0).value
                dstloc =  sheet.cell(r,1).value
                dstisp =  sheet.cell(r,2).value
                srcip  =  sheet.cell(r,3).value
                srcloc =  sheet.cell(r,4).value
                srcisp =  sheet.cell(r,5).value
                status =  sheet.cell(r,6).value
                values = (dstip.encode("utf-8"), dstloc.encode("utf-8"), dstisp.encode("utf-8"), srcip.encode("utf-8"), srcloc.encode("utf-8"), srcisp.encode("utf-8"), int(status))
                data_list.append(values)
            g_l = [gevent.spawn(self.run,data_list[i:i+100])for i in range(0,len(data_list),100)]
            gevent.joinall(g_l)
            print "插入%s条数据" % (sheet.nrows-1)
            self.cur.close()
            self.conn.close() 
    
    if __name__ == '__main__':
        if not sys.argv:
            print "Please provide file path as parameter!"
            exit(1)
        else:
            t = mysqldb('localhost', 'root', '123456', 'xiao')
  • 相关阅读:
    HTML
    初学网页
    刚学了函数,关于有无参数和返回值的四种情况的查找数字的代码
    输入五个学生的成绩,得到成绩表
    验证你的邮箱是不是qq邮箱
    摘自评论。
    LINQ Except “引用类型” 用法
    梦到钢笔
    MVC传参数给js的时候 如果是数值 变量要进行一下转换才能正确识别 例如var aaa = parseInt('@Model.ClickIndex');
    绝对路径相对路径
  • 原文地址:https://www.cnblogs.com/xingxiz/p/9907791.html
Copyright © 2011-2022 走看看