-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auto-SQL-Query-to-CSV_ANSI.py
48 lines (38 loc) · 1.46 KB
/
Auto-SQL-Query-to-CSV_ANSI.py
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
import argparse
import pandas as pd
import pymysql
def read_sql_file(path):
with open(path, 'r') as file:
return file.read()
def read_db_info_file(path):
with open(path, 'r') as file:
db_info = {}
lines = file.readlines()
for line in lines:
key, value = line.split('=')
db_info[key.strip()] = value.strip().replace("'", "")
return db_info
def execute_sql(db_info, query):
conn = pymysql.connect(host=db_info['host'],
user=db_info['user'],
password=db_info['password'],
db=db_info['database'],
port=int(db_info['port']))
return pd.read_sql_query(query, conn)
def write_to_csv(df, path):
df.to_csv(path, index=False, encoding='ansi') # 将编码参数设置为'ansi'
def main():
parser = argparse.ArgumentParser()
parser.add_argument('sql_path', help='Path to the SQL file.')
parser.add_argument('db_info_path', help='Path to the file with DB info.')
parser.add_argument('output_path', help='Path to the output CSV file.')
args = parser.parse_args()
query = read_sql_file(args.sql_path)
db_info = read_db_info_file(args.db_info_path)
df = execute_sql(db_info, query)
write_to_csv(df, args.output_path)
# 输出任务信息
print("输出文件路径:", args.output_path)
print("输出文件编码:ANSI")
if __name__ == "__main__":
main()