import pymysql # 数据库连接配置 host = '47.98.212.223' user = '6636' password = 'Mt7DiMvk7W_2' database = 'db-tm' # 要替换的字符串timichat.com .cn .net都替换为panygo.com string_to_replace = 'timichat.net' replacement_string = 'panygo.com' # 创建数据库连接 conn = pymysql.connect(host=host, user=user, password=password, database=database) try: # 创建游标对象 cursor = conn.cursor() # 获取数据库中的所有表名 cursor.execute("SHOW TABLES;") tables = cursor.fetchall() # 遍历每个表并进行数据替换操作 for table in tables: table_name = table[0] # 获取表的列名列表 cursor.execute(f"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s;", (database, table_name)) columns = [column[0] for column in cursor.fetchall()] # 构造更新语句 update_queries = [] for column in columns: update_queries.append( f"UPDATE {table_name} SET `{column}` = REPLACE(`{column}`, %s, %s);") # 执行更新操作 for query in update_queries: cursor.execute(query, (string_to_replace, replacement_string)) # 提交事务 conn.commit() print("数据替换完成。") except pymysql.Error as e: # 发生错误时回滚事务 conn.rollback() print(f"数据替换出错:{e}") finally: # 关闭游标和数据库连接 cursor.close() conn.close()