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()