从数据库读取数据,并写入到csv文件。
def get_conn(idx):
"""
连接数据库:
如果idx为0,连接数据库1
否则idx为1-31,连接另一个数据库
连接成功后,返回conn
"""
try:
if idx == 0:
conn= pymysql.connect(
host= "a",
user= "***",
password= "***",
port= ***,
db= "***")
else:
conn= pymysql.connect(
host= "b",
user= "***",
password= "***",
port= ***,
db= "***")
except Exception as e:
logger.warning(e)
finally:
return conn
def get_data_from_db():
"""
从db获取数据
并将数据写入到csv
"""
sql_v= """
select ***,***
from student_{0}
where ***
order by ***;
"""
index = 0
conn_one = get_conn(index)
sql = sql_v.format(index)#format参数对应sql_v里面的变量。本例中{0}是一个变量,index对应它
df= pd.read_sql(sql, con= conn_one)
conn_one.close()
index = 1
conn_two = get_conn(index)
for index in range(1, 32):
sql = sql_v.format(index)
df_tmp= pd.read_sql(sql, con= conn_two)
df= pd.concat([df,df_tmp])
conn_two.close()
csvPath="../data/***.csv"
df.to_csv(csvPath)
return csvPath
if __name__ =='__main__':
csvpath= getdata()