如何在python中将sql insert语句一一转换为1行和运行?

How to convert sql insert statement into 1 liner and run one by one in python?

嗨,我是 python 的新人,所以问这个问题。我在文件中插入查询。 我必须一一执行。 你能帮我吗我该怎么做?它们保存在文本文件中。它们不在 1 个班轮中。我们如何将它们制作成 1 个衬垫或将它们转换成数据帧?

示例: insert_statement.txt

INSERT INTO imedical_cleansing_rule_metadata
(
  Data_Set_Name,
  Region,
  Layer,
  Table_Name,
  Column_Name,
  Column_Seq,
  EC2_Source_Path,
  EC2_Target_Path,
  Is_Active,
  s3_tgt_path,
  Rule,
  Spectrum_Table_Name,
  Sub_Rule
)
VALUES
(
  'JGVCC',
  'ASPAC',
  'Raw_Layer',
  'DM_GENESYS_CONVERSTION_AGG',
  'queue_name',
  2,
  '/medaff/Scripts/Incoming_Src_Files/',
  '/medaff/eureka/RawLayer/JGVCC/ASPAC/Genesys/DM_GENESYS_CONVERSTION_AGG/',
  'N',
  's3://itx-{}-jg-aspac/jgvcc/Raw_Layer/DM_GENESYS_CONVERSTION_AGG/',
  'rule1;rule3;rule4;rule2;rule6;rule19',
  'lnd_jgvcc_aspac_genesys_call_detail',
  NULL
);


INSERT INTO imedical_cleansing_rule_metadata
(
  Data_Set_Name,
  Region,
  Layer,
  Table_Name,
  Column_Name,
  Column_Seq,
  EC2_Source_Path,
  EC2_Target_Path,
  Is_Active,
  s3_tgt_path,
  Rule,
  Spectrum_Table_Name,
  Sub_Rule
)
VALUES
(
  'JGVCC',
  'ASPAC',
  'Raw_Layer',
  'DM_EUAVCC_CASE_CRM',
  'parent_case_number',
  1,
  '/medaff/Scripts/Incoming_Src_Files/',
  '/medaff/eureka/RawLayer/JGVCC/ASPAC/CRM/DM_EUAVCC_CASE_CRM/',
  'N',
  's3://itx-{}-jg-aspac/jgvcc/Raw_Layer/DM_EUAVCC_CASE_CRM/',
  'rule1;rule3;rule4;rule2;rule6;rule19',
  'lnd_jgvcc_aspac_crm_activity',
  NULL
);

如果所有查询都由一个空行分隔,您可以将它们读取并存储在一个列表中:

with open("insert_statement.txt", "r") as file:
    query_list = file.read().split("\n\n\n")
    query_list = [query.replace('\n', ' ') for query in query_list]

for query in query_list:
    print(query)
    #  execute(query)