当前位置: 代码网 > it编程>前端脚本>Python > 使用Python编写一个SQL语句自动转换工具(UPDATE到INSERT转换)

使用Python编写一个SQL语句自动转换工具(UPDATE到INSERT转换)

2025年09月28日 Python 我要评论
引言在日常数据库维护和数据处理过程中,我们经常需要将update语句转换为insert语句,特别是在数据迁移、备份恢复或测试数据准备的场景中。手动转换这些sql语句不仅耗时耗力,还容易出错。本文将介绍

引言

在日常数据库维护和数据处理过程中,我们经常需要将update语句转换为insert语句,特别是在数据迁移、备份恢复或测试数据准备的场景中。手动转换这些sql语句不仅耗时耗力,还容易出错。本文将介绍如何使用python编写一个自动化工具,实现update语句到insert语句的高效转换。

问题背景

假设我们有一个包含大量update语句的sql文件:

update `xxx_detail` set `id`=1955445664111890432, `product_name`='xxx', `update_time`='2025-08-23 13:37:44' where `id`=1955445664111890432;
update `contracxxxt` set `order_sn`='xxxxx', `total_amount`=1816485 where `id`=1955445671208652800;

我们需要将这些语句转换为insert语句:

insert into `xxx_detail` (`id`, `product_name`, `update_time`) values (1955445664111890432, 'xxx', '2025-08-23 13:37:44');
insert into `contracxxxt` (`order_sn`, `total_amount`) values ('xxxxx', 1816485);

解决方案设计

核心思路

  1. 使用正则表达式匹配update语句的结构
  2. 提取表名、set子句和where条件
  3. 解析set子句中的列名和值
  4. 构建insert语句格式

关键技术点

  • 正则表达式匹配
  • 字符串处理
  • 文件读写操作
  • 错误处理机制

完整代码实现

import re
import os

def update_to_insert(sql_content):
    """将update语句转换为insert语句"""
    
    # 正则表达式匹配update语句
    update_pattern = r'update `(\w+)` set (.+?) where `id`=(\d+);'
    matches = re.findall(update_pattern, sql_content, re.dotall)
    
    insert_statements = []
    
    for table_name, set_clause, id_value in matches:
        # 解析set子句
        set_items = re.findall(r'`(\w+)`=([^,]+)(?:,|$)', set_clause)
        
        # 构建列名和值
        columns = []
        values = []
        
        for column, value in set_items:
            columns.append(f"`{column}`")
            
            # 处理null值
            value = value.strip()
            if value.upper() == 'null':
                values.append('null')
            # 处理字符串值(用单引号括起来的)
            elif re.match(r"^'.*'$", value):
                # 去除外层单引号,然后重新添加正确的单引号
                inner_value = value[1:-1]  # 去掉外层单引号
                # 转义内部单引号
                escaped_value = inner_value.replace("'", "''")
                values.append(f"'{escaped_value}'")
            # 处理数字值
            else:
                values.append(value)
        
        # 构建insert语句
        insert_sql = f"insert into `{table_name}` ({', '.join(columns)}) values ({', '.join(values)});"
        insert_statements.append(insert_sql)
    
    return insert_statements

def process_sql_file(input_file, output_file):
    """处理sql文件,将update转换为insert"""
    
    # 检查输入文件是否存在
    if not os.path.exists(input_file):
        print(f"错误:输入文件 '{input_file}' 不存在")
        return
    
    try:
        # 读取输入文件
        with open(input_file, 'r', encoding='utf-8') as f:
            sql_content = f.read()
        
        # 转换update语句
        insert_statements = update_to_insert(sql_content)
        
        # 写入输出文件
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write("-- 由update语句生成的insert语句\n")
            f.write("-- 生成时间: 2025-09-27\n")
            f.write("-- 源文件: " + input_file + "\n")
            f.write("=" * 80 + "\n\n")
            
            for i, insert_stmt in enumerate(insert_statements, 1):
                f.write(f"-- insert语句 {i}\n")
                f.write(insert_stmt + "\n")
                f.write("\n")
        
        print(f"成功生成 {len(insert_statements)} 条insert语句")
        print(f"输出文件: {output_file}")
        
    except exception as e:
        print(f"处理文件时出错: {e}")

def main():
    """主函数"""
    print("update语句转insert语句工具")
    print("=" * 40)
    
    # 输入文件路径
    input_file = input("请输入包含update语句的文件路径: ").strip()
    
    # 输出文件路径(默认在输入文件同目录下)
    if input_file:
        base_name = os.path.splitext(input_file)[0]
        output_file = f"{base_name}_insert.sql"
    else:
        output_file = "output_insert.sql"
    
    # 确认输出文件路径
    custom_output = input(f"请输入输出文件路径 (默认: {output_file}): ").strip()
    if custom_output:
        output_file = custom_output
    
    # 处理文件
    process_sql_file(input_file, output_file)

# 示例使用(直接指定文件路径)
if __name__ == "__main__":
    # 方式1:交互式输入
    # main()
    
    # 方式2:直接指定文件路径
    input_file = "./rollback_12681.sql"  # 替换为你的文件路径
    output_file = "output_insert.sql"
    process_sql_file(input_file, output_file)

代码解析

1. 正则表达式匹配

update_pattern = r'update `(\w+)` set (.+?) where `id`=(\d+);'

这个正则表达式用于匹配update语句的三个关键部分:

  • (\w+):匹配表名
  • (.+?):匹配set子句内容
  • (\d+):匹配where条件中的id值

2. set子句解析

set_items = re.findall(r'`(\w+)`=([^,]+)(?:,|$)', set_clause)

这个正则表达式用于提取set子句中的每个字段赋值对,匹配格式为:列名=值

3. 数据类型处理

代码中特别处理了三种数据类型:

  • null值:直接保留为null
  • 字符串值:去除外层单引号并转义内部单引号
  • 数字值:直接使用原值

4. 文件操作

使用with open()语句确保文件正确打开和关闭,支持utf-8编码以处理中文。

使用示例

交互式使用

运行脚本后按提示输入文件路径:

$ python update_to_insert.py
update语句转insert语句工具
========================================
请输入包含update语句的文件路径: ./rollback.sql
请输入输出文件路径 (默认: ./rollback_insert.sql): 
成功生成 25 条insert语句
输出文件: ./rollback_insert.sql

直接指定文件

修改脚本底部代码:

if __name__ == "__main__":
    input_file = "./your_update_file.sql"
    output_file = "./output_insert.sql"
    process_sql_file(input_file, output_file)

处理效果对比

转换前(update语句):

update `resource_detail` set `id`=1955445664111890432, `product_name`='热轧卷', `update_time`='2025-08-23 13:37:44' where `id`=1955445664111890432;

转换后(insert语句):

insert into `resource_detail` (`id`, `product_name`, `update_time`) values (1955445664111890432, '热轧卷', '2025-08-23 13:37:44');

扩展功能建议

  1. 支持更多where条件:当前仅支持id作为where条件,可以扩展支持其他字段
  2. 批量处理:添加对目录下多个sql文件的批量处理功能
  3. 数据库直连:添加直接连接数据库执行转换后的insert语句
  4. 语法检查:增加sql语法验证功能,确保生成的insert语句有效
  5. 进度显示:添加进度条显示处理进度

总结

本文介绍的python脚本提供了一个高效、可靠的update到insert语句转换解决方案。通过正则表达式和字符串处理技术,实现了sql语句的自动转换,大大提高了数据库维护和数据处理效率。这个工具不仅适用于文中提到的场景,还可以根据具体需求进行扩展和定制。

使用这个工具时,请注意备份原始数据,并在测试环境中验证转换结果,确保数据准确性。

以上就是使用python编写一个sql语句自动转换工具(update到insert转换)的详细内容,更多关于python sql语句自动转换的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com