如何将sql插入语句转换为1个liner并在python中逐个运行?

发布于 2025-01-09 10:03:28 字数 1382 浏览 0 评论 0原文

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

例子: 插入语句.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
);

Hi I am new in python so asking this question. I have insert query in file.
I have to execute them one by one.
Could you please help me how can I do this? They are saved in text file. They aren't in 1 liner. How can we make them in 1 liner or convert them into dataframe?

Example:
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
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风吹 2025-01-16 10:03:28

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

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)

If all queries are separated by one blank line, you could read and store them in a list:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文