检查enq1,enq2的type1id的程序
返回
# check_type1id.pl
# 程序目的:检查一个type1id下是否有两个以上相同的type
use strict;
use DBI;
my(%t,$n,@fld,$pref,@rec,%seen,@uniq,@org,$item);
# 连接数据库
$$pref{dsn} = "DBI:mysql:host=localhost;database=cookbook";
$$pref{dbh} = DBI->connect($$pref{dsn}, "cbuser", "cbpass") or die "Cannot connect to server\n";
$$pref{dbh}->do("SET NAMES utf8");
if(!$$pref{dbh}){
print "SQL read ERROR!\n";
exit;
}
print "Please input table name(enq1,enq2)=";
chop($t{table1} = );
# 取出enq1 or enq2的数据
$t{sth} = $$pref{dbh}->prepare("SELECT id,type1id FROM $t{table1}");
$t{sth}->execute;
while ( @rec = $t{sth}->fetchrow_array ) {
push(@{ $t{id_list} },$rec[0]);
push(@{ $t{type1id_list} },$rec[1]);
}
$t{sth}->finish;
# 关闭数据库
$$pref{dbh}->disconnect;
print "Table name ==> $t{table1}\n";
print " id==> org==>uniq\n";
for $n ( 0 .. $#{ $t{id_list} } ) {
$t{id1} = $t{id_list}[$n];
$t{type1id1} = $t{type1id_list}[$n];
next unless $t{type1id1} =~ /==/;
@org = split(/==/,$t{type1id1});
# 检查一个配列中是否有相同的项目
%seen = ();
@uniq = ();
foreach $item (@org) {
push(@uniq,$item) unless $seen{$item}++;
}
if ( $#org != $#uniq ) {
printf ("%04d==>%04d==>%04d\n",$t{id1},$#org,$#uniq);
}
}
返回