自动识别json结构数据转换为建表SQL语句(Python 代码 示例数据来源于某眼查)
首先说下我的思路
1.先把json数据的格式复制到excel表内,方便读取(格式如图)
2.读取电脑系统路径下的excel文件,路径请修改file_path参数,注意文件命名和路径
3.自动转换为建表SQL语句并且打印出来(工具用的pycharm)
以下是python代码示例,可复制使用
import pandas as pd# 从 Excel 文件读取数据
def read_excel_to_json(file_path):df = pd.read_excel(file_path)json_data = {}tables = {} # 存储所有表的字典current_table = Nonecurrent_table_name = Nonefor _, row in df.iterrows():field_name = row['返回值字段']field_type = row['字段类型']field_length = row.get('字段说明', None)field_comment = row.get('备注', '')# 将 '日期' 替换为 'DATETIME'if pd.notna(field_length) and '日期' in field_length:field_length = 'DATETIME'if pd.notna(field_length) and '时间戳' in field_length:field_length = 'DATETIME' # 处理 _child 字段,开始新表if field_name == '_child':if current_table_name:# 将当前表保存tables[current_table_name] = current_tablecurrent_table_name = f"{len(tables) + 1}_child_table" # 生成新的子表名current_table = {} # 新建一个子表continueif field_type == 'Object' and field_comment != '无用':if current_table_name:# 将当前表保存tables[current_table_name] = current_tablecurrent_table_name = f"{len(tables) + 1}_child_table" # 生成新的子表名current_table = {} # 新建一个子表continue# 只添加字段说明不为空且备注不为“无用”的字段if pd.notna(field_length) and field_length and field_comment != '无用':if current_table is not None:# 如果是子表字段current_table[field_name] = {"type": field_length,"comment": field_comment}else:# 如果是主表字段json_data[field_name] = {"type": field_length,"comment": field_comment}# 保存最后一个表if current_table_name and current_table:tables[current_table_name] = current_tablereturn json_data, tablesdef generate_create_table_sql(json_data, table_name, is_child=False):sql = f"CREATE TABLE {table_name} (\n"max_field_length = max(len(field) for field in json_data.keys()) if json_data else 0max_type_length = max(len(properties['type']) for properties in json_data.values()) if json_data else 0max_comment_length = max(len(properties['comment']) for properties in json_data.values()) if json_data else 0for field, properties in json_data.items():field_type = properties.get("type")comment = properties.get("comment", '')# 纵向对齐字段、字段类型和注释sql += f" {field} {' ' * (max_field_length - len(field))} {field_type} {' ' * (max_type_length - len(field_type) + 1)} COMMENT '{comment}',\n"# 仅对子表添加 bizadmin_idif is_child:sql += f" bizadmin_id {' ' * (max_field_length - len('bizadmin_id'))} INT {' ' * (max_type_length - len('INT') + 1)} COMMENT '公司ID',\n"# 添加创建和更新时间字段sql += f" create_time {' ' * (max_field_length - len('create_time'))} DATETIME {' ' * (max_type_length - len('DATETIME') + 1)} COMMENT '数据创建时间',\n"sql += f" update_time {' ' * (max_field_length - len('update_time'))} DATETIME {' ' * (max_type_length - len('DATETIME') + 1)} COMMENT '数据更新时间'\n"sql = sql.rstrip(',\n') + '\n);' # 移除最后一个逗号return sql# 文件路径
file_path = r'D:\file\工商信息表结构.xlsx' # 替换为你的文件路径
json_data, child_tables = read_excel_to_json(file_path)# 生成主表 SQL 语句
table_name = "main_table" # 替换为你的主表名
create_table_sql = generate_create_table_sql(json_data, table_name)# 生成子表 SQL 语句
for child_table_name, child_table in child_tables.items():create_child_table_sql = generate_create_table_sql(child_table, child_table_name, is_child=True)create_table_sql += f"\n-- 子表创建语句\n{create_child_table_sql}"# 输出 SQL 语句
print(create_table_sql)