实现将mysql日志文件输出结果写入另一个数据库中
这一节主要是基于python的pymysql模块实现向数据库中插入一条数据实现将输出的数据重新写入另一个数据库。实现:添加了一个insert_merid函数。实现连接mysql并往里面插入查询结果数据。#!/usr/bin/env python# -*- coding: utf-8 -*-# @Author: Zhou Jiaanimport osimport time...
·
这一节主要是基于python的pymysql模块实现向数据库中插入一条数据实现将输出的数据重新写入另一个数据库。
实现:添加了一个insert_merid函数。实现连接mysql并往里面插入查询结果数据。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : Zhou Jiaan
import os
import time
import re
import pymysql
def readInterfacelog(merid):
with open('/var/log/interfacelog.log', 'r') as f:
lines = f.readlines()
for line in lines[2:-1:1]:
interfacelog = re.split(r'\s*', line)
sendorderid = interfacelog[0]
price100 = interfacelog[1]
mob = interfacelog[2]
# print(sendorderid, price100, merid, mob)
# os.system('/usr/local/007ka/bin/MsgSrvClient -h 10.20.10.212 -p 6004 -w0 -e "IFTran SlowInt 1 %s %s %s NA 1 5000 120 NA 22 NA NA %s NA NA NA";'%(sendorderid,price100,merid,mob))
insert_merid(merid,interfacelog)
time.sleep(0.1)
def insert_merid(merid,interfacelog):
conn = pymysql.connect(host="10.1.11.37", port=3306, user="root", passwd="password", db="test",
charset="utf8mb4")
cur = conn.cursor()
cur.execute(
"INSERT INTO interfacelog(id,sendorderid,price,merid,mob)VALUES(NULL,{0},{1},{2},{3});".format(interfacelog[0],interfacelog[1],merid,interfacelog[2]))
conn.commit()
conn.close()
def main():
# merid=sys.argv[1]
try:
merid_num = sys.argv[1]
except Exception as e:
merid_num = input("请输入merid:")
else:
print("输出merid_num:{}".format(merid_num))
finally:
merid = merid_num
print(merid)
readInterfacelog(merid)
if __name__ == '__main__':
main()
更多推荐
已为社区贡献7条内容
所有评论(0)