Python 批量替换MySQL数据库内全部数据表全部字段的字符串

#encoding:utf-8

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="数据库用户名",
  password="数据库密码",
  database="数据库名"
)

mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
tables = mycursor.fetchall()

for table in tables:
    table_name = table[0]
    #mycursor.execute(f"USE {table_name}")  # 使用指定的表所在的数据库
    mycursor.execute(f"SHOW COLUMNS FROM {table_name}")  # 获取表的所有列信息
    columns = mycursor.fetchall()
    for column in columns:
        column_name = column[0]
        sql = f"UPDATE {table_name} SET {column_name} = REPLACE({column_name}, '旧字符串', '新字符串')"
        mycursor.execute(sql)
    mydb.commit()

mycursor.close()
mydb.close()

Python 批量替换MySQL数据库内全部数据表全部字段的字符串