【发布时间】:2009-05-23 23:38:57
【问题描述】:
我想替换特定数据库的每个列和每个表中的字符串。有没有办法做到这一点 ?
提前致谢:)
编辑:那是因为我在某些地方发现了 Gibberish,我需要几天时间才能一个一个地修复它
【问题讨论】:
我想替换特定数据库的每个列和每个表中的字符串。有没有办法做到这一点 ?
提前致谢:)
编辑:那是因为我在某些地方发现了 Gibberish,我需要几天时间才能一个一个地修复它
【问题讨论】:
不做一些编程就不行。
最简单的方法是使用您喜欢的语言中的 show/describe 命令并在结果集(列/表的名称)上运行,并从中创建要在数据库上运行的 UPDATE 查询列表。
你为什么不写它并开源它,你不是第一个寻找类似东西的人(或者谷歌它,可能在某个地方已经准备好了一个脚本)。
【讨论】:
【讨论】:
当我们在 Wordpress 网站上更改域时,我自己也在寻找这个。没有一些编程就无法完成,所以这就是我所做的。
<?php
header("Content-Type: text/plain");
$host = "localhost";
$username = "root";
$password = "";
$database = "mydatabase";
$string_to_replace = 'old.example.com';
$new_string = 'new.example.com';
// Connect to database server
mysql_connect($host, $username, $password);
// Select database
mysql_select_db($database);
// List all tables in database
$sql = "SHOW TABLES FROM ".$database;
$tables_result = mysql_query($sql);
if (!$tables_result) {
echo "Database error, could not list tables\nMySQL error: " . mysql_error();
exit;
}
echo "In these fields '$string_to_replace' have been replaced with '$new_string'\n\n";
while ($table = mysql_fetch_row($tables_result)) {
echo "Table: {$table[0]}\n";
$fields_result = mysql_query("SHOW COLUMNS FROM ".$table[0]);
if (!$fields_result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($fields_result) > 0) {
while ($field = mysql_fetch_assoc($fields_result)) {
if (stripos($field['Type'], "VARCHAR") !== false || stripos($field['Type'], "TEXT") !== false) {
echo " ".$field['Field']."\n";
$sql = "UPDATE ".$table[0]." SET ".$field['Field']." = replace(".$field['Field'].", '$string_to_replace', '$new_string')";
mysql_query($sql);
}
}
echo "\n";
}
}
mysql_free_result($tables_result);
?>
希望它对将来遇到此问题的人有所帮助:)
【讨论】: