前缀更改后Drupal MySQL错误

时间:2014-01-03 11:32:10

标签: database drupal prefix

我设置了一个drupal网站,该网站首先有一个前缀但我在数据库中转储了一个没有前缀的旧数据库导出。所以我从settings.php中删除了它,如下所示:

$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => 'db_name_here',    //hidden
      'username' => 'username_here',   //hidden
      'password' => '*******',         //hidden
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',                  //prefix used to be here 'example_'
    ),
  ),
);

然而问题是,当我连接到数据库时,它似乎仍然在寻找带有前缀的表。我做错了什么?

PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 
'db_name_here.example_semaphore' doesn't exist: SELECT expire, value FROM
{semaphore} WHERE name = :name; Array ( [:name] => variable_init )
in lock_may_be_available() (line 167 of /home/public_html/demo/site/includes/lock.inc).

1 个答案:

答案 0 :(得分:1)

当您使用数据库前缀安装Drupal时,所有数据库表也将获得该前缀,以便删除需要重命名所有数据库表的前缀。

以下脚本可以帮助您完成此操作。

<?php
// current table prefix to be removed
$prefix = "drup_";
// echo generated statments rather then run them
$pretend = FALSE;
/////////////////////////////////////////////////////////////////////
$table_list = db_query("SHOW TABLES");
$prefix = strtolower($prefix);
foreach ($table_list as $r) {
  $r = (array)$r;
  $table_old = strtolower(current($r));
  // check for $prefix on this table
  if(substr($table_old,0,strlen($prefix)) == $prefix) {
    $table_new = substr($table_old, strlen($prefix));
    // first drop $table_new incase it already exists
    $clean_sql = "DROP TABLE IF EXISTS {$table_new}";
    // rename prefix table to standard/nonprefix name
    $rename_sql = "RENAME TABLE {$table_old} TO {$table_new}";
    if($pretend) {
      print $clean_sql."\n";
      print $rename_sql."\n";
    } else {
      if(!db_query($clean_sql)) {
        die("Aborting - $clean_sql \n");
      }
      if(!db_query($rename_sql)) {
        die("Aborting - $rename_sql \n");
      }
    }
  } else {
    print "$table_old skipped \n";
  }
}
print "\nDone \n\n";
?>