• python 对比数据库,生成sql


    1. import pymysql
    2. def get_table_structure(connection, cursor, database, table):
    3. # 获取表结构
    4. query = f"SHOW COLUMNS FROM {database}.{table}"
    5. cursor.execute(query)
    6. return cursor.fetchall()
    7. def generate_create_table(cursor, table_structure, database, table):
    8. # 生成创建表的SQL语句,包含字段约束、默认值和主键信息
    9. columns = []
    10. for column in table_structure:
    11. column_name = column[0]
    12. column_type = column[1]
    13. is_nullable = "NOT NULL" if column[2] == "NO" else ""
    14. default_value = f"DEFAULT {column[4]}" if column[4] is not None else ""
    15. columns.append(f"`{column_name}` {column_type} {is_nullable} {default_value}")
    16. # 查找主键信息
    17. cursor.execute(f"SHOW KEYS FROM {table} WHERE Key_name = 'PRIMARY'")
    18. primary_key = cursor.fetchone()
    19. if primary_key:
    20. columns.append(f"PRIMARY KEY (`{primary_key[4]}`)")
    21. return f"CREATE TABLE `{database}`.`{table}` ({', '.join(columns)});"
    22. def generate_add_or_modify_column(connection, cursor, database, table, column_structure):
    23. # 生成添加字段或修改字段的SQL语句
    24. alter_statements = []
    25. for column_info in column_structure:
    26. # column_name, column_type, is_nullable, default_value = column_info
    27. # default_clause = f"DEFAULT {default_value}" if default_value is not None else ""
    28. column_name = column_info[0]
    29. column_type = column_info[1]
    30. is_nullable = "NOT NULL" if column_info[2] == "NO" else ""
    31. default_value = f"DEFAULT {column_info[4]}" if (column_info[4] is not None and column_info[4]) else "DEFAULT NULL"
    32. # 检查字段是否已经存在
    33. if is_column_exists(cursor, database, table, column_name):
    34. # 字段存在,生成修改字段的SQL语句
    35. alter_statements.append(
    36. f"ALTER TABLE `{database}`.`{table}` MODIFY COLUMN `{column_name}` {column_type} {is_nullable} {default_value} ;")
    37. else:
    38. # 字段不存在,生成添加字段的SQL语句
    39. alter_statements.append(
    40. f"ALTER TABLE `{database}`.`{table}` ADD COLUMN `{column_name}` {column_type} {is_nullable} {default_value} ;")
    41. return alter_statements
    42. def is_column_exists(cursor, database, table, column_name):
    43. # 检查字段是否存在于目标表中
    44. query = f"SHOW COLUMNS FROM `{database}`.`{table}` LIKE '{column_name}'"
    45. cursor.execute(query)
    46. return cursor.fetchone() is not None
    47. if __name__ == "__main__":
    48. dba ="test";
    49. dbb = "test1";
    50. # 连接数据库A
    51. db_a = pymysql.connect(host="127.0.0.1", port=3307, user="root", password="123456", database=dba)
    52. cursor_a = db_a.cursor()
    53. # 连接数据库B
    54. db_b = pymysql.connect(host="127.0.0.1", port=3307, user="root", password="123456", database=dbb)
    55. cursor_b = db_b.cursor()
    56. # 获取数据库A中的表
    57. cursor_a.execute("SHOW TABLES")
    58. database_a_tables = [table[0] for table in cursor_a.fetchall()]
    59. # 获取数据库B中的表
    60. cursor_b.execute("SHOW TABLES")
    61. database_b_tables = [table[0] for table in cursor_b.fetchall()]
    62. # 比较两个数据库的表结构并生成SQL语句
    63. for table in database_a_tables:
    64. if table not in database_b_tables:
    65. # 表在库B中缺失,生成创建表语句
    66. table_structure = get_table_structure(db_a, cursor_a,dba, table)
    67. create_table_sql = generate_create_table(cursor_a, table_structure, dbb, table)
    68. print(create_table_sql)
    69. else:
    70. # 表在库B中存在,比较字段
    71. columns_a = get_table_structure(db_a, cursor_a, dba, table)
    72. columns_b = get_table_structure(db_b, cursor_b,dbb, table)
    73. # 字段差异
    74. column_diff = [col for col in columns_a if col not in columns_b]
    75. # 生成添加字段或修改字段的SQL语句
    76. alter_column_sqls = generate_add_or_modify_column(db_b, cursor_b, dbb, table, column_diff)
    77. for alter_column_sql in alter_column_sqls:
    78. print(alter_column_sql)
    79. # 关闭数据库连接
    80. db_a.close()
    81. db_b.close()

  • 相关阅读:
    RabbitMQ的Windows版安装教程
    前端开发:JS中的Window对象详解
    实现一个博客系统----使用模板引擎技术
    docker之Harbor私有仓库
    SSM项目整合 文件上传
    GoConvey中Convey嵌套时的执行顺序
    高级测试:如何使用Flink对Strom任务的逻辑功能进行复现测试?
    《C++避坑神器·十九》C++多线程使用,啥也不懂看它就对了
    高并发场景下常见的限流算法及方案介绍
    【JAVA-Day38】深入了解Java常用类 String:字符串操作的技巧和方法
  • 原文地址:https://blog.csdn.net/ZhongGuoZhiChuang/article/details/134528737