python获取并解析远程sqllite,插入mysql

import os
import sqlite3
import MySQLdb


def getfilename():
   if os.path.exists('/home/ftpu/getsqlite/base.txt') is False:
            f=open('/home/ftpu/getsqlite/base.txt','w')
            f.close()
           
   baselists=open('/home/ftpu/getsqlite/base.txt').readlines()
   checklist=[]
   for base in baselists:
     base=base.replace(remotedir,localdir)
     base=base.replace('\n','')
     checklist.append(base)
   
   getupdate='sshpass -p Ftp password ssh -q -o StrictHostKeyChecking=no ftpu@remoteIP sh /home/hz6/listfile.sh remotedir > outputidr + '/update.txt'   
   os.system(getupdate)
   
   for file in open(outputdir + '/update.txt').readlines():
      
      if file.strip() in checklist:
         pass
      else:
         file=file.strip()
         
         if file[-2:]=='db':
                        
            remotefile=file
            rt=remotefile.rfind('/')
            remotepath=remotefile[:rt]

            localfile=str(remotefile).replace(localdir,remotedir)
    
            st = localfile.rfind('/')
            localpath=localfile[:st]         
            localfile=localfile.strip()

            if os.path.isdir(localpath):
               pass
            else:
               os.makedirs(localpath)

            if os.path.exists(localfile):
               os.remove(localfile)
            print(1,localfile)

            trans="sshpass -p Ftp password scp ftpu@remoteip:" + remotefile.strip() + " " + localpath.strip()
            os.system(trans)
    
            sqlitedata(localfile)
            

def sqlitedata(localfile):
    chmd='chmod 700 ' +  localfile
    os.system(chmd)
    chow='chown -R ftpu:ftpu ' + localfile
    os.system(chow)
    localfile = localfile.strip()
    
    conn=sqlite3.connect(localfile)
    sqldb=conn.cursor()
    rows=sqldb.execute('select rowid,sequence,station_name,device_id,filament_name,yellow_light_status,green_light_status,red_light_status,white_light_status,blue_light_status,leadyellow_light_status,channel1_value,channel2_value,rev_time from filament_record')

    records=''
    for row in rows:
       record='('
       
       for col in row:
          if type(col) == int:
              record+=str(col)+','
          else:
              record+="'" + col + "',"
        
       record=record[:-1]+'),'
       records+=record

    records=records[:-1]    
    InsertMySQL(localfile,records)

def InsertMySQL(localfile,records):
    con = MySQLdb.Connect(host = ip, user =user, passwd = password,db='db',use_unicode=True, charset="utf8")
    cur = con.cursor()

    getrowid='select max(rowid) from t_name'
    cur.execute(getrowid)

    rowid=0
    for row in cur.fetchall():
      
      if row == (None,):
         rowid = 0
      else:
         rowid = int(row[0])
    
    deletecmd='delete from filament_record'
    cur.execute(deletecmd)
        
    insertcmd = 'insert into filament_record VALUES '
    insertcmd += records
    cur.execute(insertcmd)
    
    updatecmd = 'insert into t_name select sequence+' + str(rowid) + ' as rowid,sequence,station_name,device_id,filament_name,yellow_light_status,green_light_status,red_light_status,white_light_status,blue_light_status,leadyellow_light_status,channel1_value,channel2_value,rev_time from filament_record'
    cur.execute(updatecmd)
    localfile+='\n'
    with open('/home/ftpu/getsqlite/base.txt','a') as f:
      f.write(localfile)    
    print(2,localfile)
    cur.close()
    con.commit()
    con.close()

if __name__ == "__main__":
    
    getfilename()
    
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值