python执行sql语句

 

  dbname = 'db.sqlite3'
    dbpath = 'D:\\pyweb\\sf\\db.sqlite3'
    csvpath = pspath

    #custom thread number
    tnum = 20
    hvsrvs_all =hvsrvs.objects.all()
    serverips=[]
    #ls = []
    for hvsrv in hvsrvs_all:
         Phy_ServerIP = hvsrv.serverip
         serverips.append(Phy_ServerIP)


    for ips in lstg(tnum,serverips):
        threads=[]

        for Phy_ServerIP in ips:
            #print Phy_ServerIP
            t = threading.Thread(target=GetVMs,args=(PS_GetVMs,Phy_ServerIP))
            t.setDaemon(True)
            t.start()
            threads.append(t)  
        for t in threads:
            t.join()

    sql_insert = '''insert into sinfors_hvvms values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'''
    conn = sqlite3.connect(dbpath)
    cu = conn.cursor()
    datas=[]
    i = 1
    for csvf in os.listdir(csvpath):
        if os.path.splitext(csvf)[0].startswith('queryresult-') and os.path.splitext(csvf)[1] == '.csv':
            csvfile = os.path.join(csvpath,csvf)
            #print csvfile
            cf = open(csvfile)
            cf.readline()
            
            for l in csv.reader(cf):
                l.insert(0,i)
                i=i+1
                datas.append(tuple(l))
            cf.close()
            #print csvfile
            os.remove(csvfile)
    #print datas
    if len(datas) > 0:
        cu.execute('delete from sinfors_hvvms')
        conn.commit()
        for data in datas:
            cu.execute(sql_insert,data)

        conn.commit()
        cu.execute('select * from sinfors_hvvms')
        cu.close()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值