2010-04-15 35 views
1

您可以告诉我如何循环&在Perl中重命名MySQL表。谢谢。如何在Perl中循环并重新命名MySQL表

我的代码片段连接

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(); 
} 
+0

什么是这个问题的downvote? – 2010-04-15 14:59:53

回答

0

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

my @tables = map @$_, @{ $dbh->selectall_arrayref('SHOW TABLES') }; 

for my $table (@tables) { 
    $dbh->do("RENAME TABLE $table TO new_${table}"); 
} 

希望这有助于。

1

我假设你的表格列表生活在DATA

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

你可能也想看看perldoc DBI

+0

++用于占位符。 – daotoad 2010-04-16 16:05:03