import pandas as pd
def generate_partition_table(excel_file, partition_field):
# 读取Excel文件
df = pd.read_excel(excel_file)
# 获取表名称
table_name = df.iloc[0, 0]
# 获取字段信息
columns = df.iloc[1:, 1].tolist()
data_types = df.iloc[1:, 2].tolist()
is_primary_keys = df.iloc[1:, 3].tolist()
is_not_nulls = df.iloc[1:, 4].tolist()
is_indexes = df.iloc[1:, 5].tolist()
# 创建分区表的SQL语句
create_table_sql = f"CREATE TABLE {table_name} (\n"
# 添加字段、数据类型和约束
for column, data_type, is_primary_key, is_not_null, is_index in zip(columns, data_types, is_primary_keys, is_not_nulls, is_indexes):
# 添加字段和数据类型
create_table_sql += f" {column} {data_type},\n"
# 添加主键约束
if is_primary_key == "Y":
create_table_sql += f" PRIMARY KEY ({column}),\n"
# 添加非空约束
if is_not_null == "Y":
create_table_sql += f" {column} {data_type} NOT NULL,\n"
# 添加索引
if is_index == "Y":
create_table_sql += f" INDEX ({column}),\n"
# 添加分区字段
create_table_sql += f" PARTITION BY {partition_field}\n"
# 添加分区表的结尾
create_table_sql += ");"
# 打印生成的SQL语句
print(create_table_sql)
指定Excel文件路径和分区字段
excel_file = “path/to/excel_file.xlsx”
partition_field = “partition_field_name”
调用函数生成分区表
generate_partition_table(excel_file, partition_field)