對於在Perl中使用MySQL和總新手而言,這是一個相當新的東西,但我試圖破解別人的腳本來幫助我。我從here得到了腳本。它迄今爲止看起來不錯,但由於表有一些外鍵檢查正在進行,所以它失敗了。我可以去通過phpmyadmin和嘗試,並刪除它們一個接一個,但是這需要永遠和不得不做的是我第三次:(我的查詢,可以在這個腳本ammended包括:禁用外鍵檢查刪除InnoDB表Perl Perl腳本
它運行drop table命令之前?我試圖通過按照劇本,但無法找到腳本(可能是由於無知/缺乏瞭解)的最終命令的一部分。任何幫助極大的讚賞。
#!/usr/bin/perl
use strict;
use DBI;
my $hostname = '';
my $database = '';
my $username = '';
my $password = '';
my $dbh = DBI->connect("dbi:mysql:${database}:$hostname",
$username, $password) or die "Error: $DBI::errstr\n";
my $sth = $dbh->prepare("SHOW TABLES");
$sth->execute or die "SQL Error: $DBI::errstr\n";
my $i = 0;
my @all_tables =();
while(my $table = $sth->fetchrow_array)
{
$i++;
print "table $i: $table\n";
push @all_tables, $table;
}
my $total_table_count = $i;
print "Enter string or regex to match tables to "
. "delete (won't delete yet): ";
my $regex = <STDIN>;
chomp $regex;
$i = 0;
my @matching_tables =();
foreach my $table (@all_tables)
{
if($table =~ /$regex/i)
{
$i++;
print "matching table $i: $table\n";
push @matching_tables, $table;
}
}
my $matching_table_count = $i;
if($matching_table_count)
{
print "$matching_table_count out of $total_table_count "
. "tables match, and will be deleted.\n";
print "Delete tables now? [y/n] ";
my $decision = <STDIN>;
chomp $decision;
$i = 0;
if($decision =~ /y/i)
{
foreach my $table (@matching_tables)
{
$i++;
print "deleting table $i: $table\n";
my $sth = $dbh->prepare("DROP TABLE $table");
$sth->execute or die "SQL Error: $DBI::errstr\n";
}
}
else
{
print "Not deleting any tables.\n";
}
}
else
{
print "No matching tables.\n";
}
您是否想要無限期地刪除外鍵,或者在您使用數據庫時暫時不要檢查任何內容? – Wrikken 2010-07-19 15:30:46
(注意,perl代碼可能是:'my $ sth = $ dbh-> prepare(「SET FOREIGN_KEY_CHECKS = 0;」); $ sth-> execute') – Wrikken 2010-07-19 15:32:01
您可以使用' - > do'準備/執行對的地方,如果你永遠不會再使用準備好的句柄:) – hobbs 2010-07-20 06:41:31