【问题标题】:How to Loop & rename MySQL table in Perl如何在 Perl 中循环和重命名 MySQL 表
【发布时间】:2010-04-15 13:33:44
【问题描述】:

你能教我如何在 Perl 中循环和重命名 MySQL 表吗?谢谢。

附上我的代码sn-p

use strict; 
use warnings; 
use DBI; 

my $dbh = DBI->connect( 
    'DBI:mysql:database=dbdev;host=localhost', 
    'dbdev', 
    'dbdevpw', 
    { RaiseError => 1, AutoCommit => 1 }, 
); 

my $sql = RENAME TABLE old_table TO new_table; 
my $sth = $dbh->prepare($sql); 

while (<DATA>){ 
    chomp; 
    // How to implement the Rename all the old tables with the while loop.


    $sth->execute(); 
} 

【问题讨论】:

  • 这个问题的反对意见是什么?

标签: mysql perl loops dbi


【解决方案1】:

我假设您的表列表位于 DATA 中。

while (<DATA>){ 
    chomp; 
     $dbh->do("RENAME TABLE ? TO ?", undef, $_, "new_" . $_);
} 

您可能还想看看perldoc DBI

【讨论】:

    【解决方案2】:

    此代码可用于重命名数据库中的所有表:

    my @tables = map @$_, @{ $dbh->selectall_arrayref('SHOW TABLES') };
    
    for my $table (@tables) {
        $dbh->do("RENAME TABLE $table TO new_${table}");
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2010-12-29
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 2012-09-04
      • 1970-01-01
      • 2012-12-06
      相关资源
      最近更新 更多