-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetSqlInsert.py
More file actions
103 lines (81 loc) · 3.29 KB
/
getSqlInsert.py
File metadata and controls
103 lines (81 loc) · 3.29 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import sys
"""
@author: qiuYeBai
@time: 2024/11/24
license: (C) Copyright 2024, qiuYeBai
功能:
查询语句:将终端数据形式转换成insert语句
"""
def process_line(line):
"""
处理单行数据:去掉两端 '| ' 和 ' |',按 ' | ' 分割,去掉单元格多余空格。
如果单元格是空字符串,则处理为 '';
如果是 NULL 或 null,则处理为 NULL(不加单引号)。
其他值加上单引号。
:param line: 原始行数据
:return: 处理后的行数据列表
"""
line = line.strip().strip('|') # 去掉前后 '|'
cells = [
"NULL" if cell.strip().lower() == "null" else
"''" if not cell.strip() else
f"'{cell.strip()}'"
for cell in line.split(' | ')
]
return cells
def process_line_columns(line):
line = line.strip().strip('|') # 去掉前后 '|'
cells = [f"{cell.strip()}" for cell in line.split(' | ')]
return cells
# 辅助函数:获取列名行
def get_columns_line_index(lines):
for i, line in enumerate(lines):
if '|' in line and not line.strip().startswith('+'):
return i
raise ValueError("未找到列名行")
# 辅助函数:获取最后一行行号
def get_last_line_index(lines):
for i in range(len(lines) - 1, -1, -1): # 从文件末尾向前查找
line = lines[i].strip()
# 找到最后一个数据行(包含 | 且不是分隔线的行)
if '|' in line and not line.startswith('+'):
return i + 1 # 返回下一行的索引,这样切片时能包含最后一行数据
raise ValueError("未找到数据结束行")
def convert_mysql_output_to_insert(file_path):
"""
将 MySQL 查询结果转换为 INSERT INTO 语句并保存为 .sql 文件
:param file_path: 输入文件路径
"""
table_name = os.path.splitext(os.path.basename(file_path))[0] # 用文件名作为表名
output_file = f"{table_name}.sql" # 输出文件名
with open(file_path, 'r', encoding='utf-8') as file:
# 去掉空行后再处理
lines = [line for line in file.readlines() if line.strip()]
columns_line_index = get_columns_line_index(lines)
last_line_index = get_last_line_index(lines)
# 排除第一行、第三行和最后一行,提取列名和数据行
# 实际上还有考虑空行等
columns_line = lines[columns_line_index].strip() # 第二行是列名
data_lines = lines[columns_line_index + 2:last_line_index] # 数据行是从第4行到倒数第2行
# 提取列名
columns = process_line_columns(columns_line)
# 处理每行数据
insert_statements = []
for line in data_lines:
values = process_line(line)
insert_statement = f"INSERT INTO {table_name} ({', '.join(columns)}) VALUES ({', '.join(values)});"
insert_statements.append(insert_statement)
# 保存到输出文件
with open(output_file, 'w', encoding='utf-8') as output:
output.write('\n'.join(insert_statements))
print(f"SQL 文件已生成: {output_file}")
# 示例调用
# a.txt
if __name__ == '__main__':
if len(sys.argv) < 2:
print("请提供文件名参数!")
print("使用方法: python util.py a.txt")
sys.exit(1)
file_name = sys.argv[1]
convert_mysql_output_to_insert(file_name)