需要使用生成表语法读取CSV文件的.SQL文件的脚本。
表名,列名和长度在CSV文件中提供。
示例:
TABLE_NAME,COLUMN_NAME,DATA_TYPE,DATA_LENGTH
xyz name varchar 250
xyz name varchar 250
pqr age int
我的CSV文件中有260个表格,可以读取这些表格并将其转换为创建表格语法。
答案 0 :(得分:0)
类似这样的东西:
csv_path = 'C:\\path_to_your_csv_file'
tables = {}
is_first_row = True
with open(csv_path) as f:
for line in f:
if is_first_row:
is_first_row = False
continue
table_name, column_name, data_spec = [str(e).rstrip() for e in line.split(',', maxsplit=2)]
if table_name not in tables.keys():
tables[table_name] = []
data_type = data_spec if ',' not in data_spec else '('.join(data_spec.split(',')) + ')'
tables[table_name].append(column_name + ' ' + data_type)
result='\n'.join(['CREATE TABLE ' + table_name + ' (' + (', '.join(tables[table_name])) + ');' for table_name in tables.keys()])
print(result)
输出:
CREATE TABLE pqr (age int);
CREATE TABLE xyz (name varchar(250), surname varchar(250));