del_gpsdb_by_sn.py
2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python
# -*- coding:utf-8 -*-
from urllib import parse
import pandas as pd
import pymysql,traceback
from sqlalchemy import create_engine
#清理接入侧的设备
#线上gpsdb
gpsdb_conn = pymysql.Connect(host='192.168.5.21', port=3306, user='c_gps',passwd='942b319271da', db='gpsdb', charset='utf8')
# gpsdb_conn = pymysql.connect(host='192.168.3.222', port=3307, user='root',passwd='Msd^*$@online', db='gpsdb', charset='utf8')
#从库
# gpsdb_conn_slave = pymysql.connect(host='192.168.5.22', port=3306, user='user_chenqj', passwd='MtWSIfaf', db='gpsdb',charset='utf8')
#gpsdb备份
conn_gpsdb_test = pymysql.connect(host='192.168.3.222', port=3307, user='root',passwd='Msd^*$@online', db='gpsdb', charset='utf8')
def insertsn(snList):
print(f"{len(snList)}")
sql = f"insert into t_clear_hbase_sn (DEVICENO) values"
bFirst = True
for sn in snList:
if bFirst is False:
sql+=','
sql += f"('{sn}')"
bFirst = False
cursor = gpsdb_conn.cursor()
try:
cursor.execute(sql)
gpsdb_conn.commit()
except Exception:
val = traceback.format_exc() # 获取异常信息
print(val)
def add_delete_sn():
df = pd.read_excel("可删除的设备号.xlsx")
print(df.columns)
df['sn'] = df['sn'].astype(str)
snList = list(df['sn'])
total = len(snList)
sumCount = int(total/10000)
sn_df = insertsn(snList[0:10000])
for n in range(sumCount):
startRow = (n+1)*10000
endRow = (n+2)*10000
insertsn(snList[startRow:endRow])
def backup_track_info_to_test():
sql = "SELECT * FROM t_tracker_info WHERE DEVICENO IN (SELECT DEVICENO FROM t_clear_hbase_sn tchs)"
cursor = gpsdb_conn.cursor()
cursor.execute(sql)
result = cursor.fetchall()
df = pd.DataFrame(list(result),columns=('ID','DEVICENO','FACID','TYPE','IP','CREATETIME','UPDATETIME'))
df.to_sql("t_tracker_info_clear_bak",
con=create_engine(f'mysql+pymysql://root:{parse.quote_plus("Msd^*$@online")}@192.168.3.222:3307/gpsdb?charset=utf8'),
index=False,
if_exists="append")
if __name__ == '__main__':
# 导入要删除的设备
# add_delete_sn()
# 把要删除的数据备份到测试环境:t_tracker_info_clear_bak
# backup_track_info_to_test()
# 从track_info里删除信息
sql = "delete from t_tracker_info where DEVICENO in (select DEVICENO FROM t_clear_hbase_sn)"
cursor = gpsdb_conn.cursor()
try:
cursor.execute(sql)
gpsdb_conn.commit()
except Exception:
val = traceback.format_exc() # 获取异常信息
print(val)