对于多sql语句查询,使用的方法是将脚本都放入txt中,通过一个循环,将语句依次读入,并执行
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pymysql;
import sshtunnel;
def get_sshData2(sql):
with sshtunnel.SSHTunnelForwarder(
ssh_address_or_host='***.**.**.***',
ssh_port=**,
ssh_username='*****',
ssh_password='******',
remote_bind_address=('*****',3306)
) as tunnel:
conn=pymysql.connect(host='****',user='****', passwd='*****', db='****', port=*****, charset='utf8');
cursor = conn.cursor();
cursor.execute(sql);
# 使用 fetchone() 方法获取单条数据.
results = cursor.fetchall()
print(results)
# 关闭数据库连接
conn.close()
# 返给结果给函数调用者。
return results
def testSSHDB(sql):
#sql = "select from_unixtime(reg_time, '%Y-%m-%d')as dd,count(*) from deayou_users where from_unixtime(reg_time, '%Y-%m')='2018-02'group by dd ";
# get_sshData(sql);
results=get_sshData2(sql);
#调用上面的方法 将数据写入
f2 = open("C:/Users/Miller/Desktop/sql_1.txt","r")
lines = f2.readlines()
for line3 in lines:
line3 = line3.strip('\n');
if line3!='':
testSSHDB(line3)
else: '停止执行'