我有一个散列哈希数组,我正在对其进行区分,并将其相应地插入到单个表LOCATION中,使其适合列名。代码如下:
my $dsn = "DBI:mysql:saferoute";
my $username = "root";
my $password = "password";
my %attr = (PrintError=>0,RaiseError=>1);
my $dbh = DBI->connect($dsn,$username,$password,\%attr);
while (my ($year, $year_ref) = each(%$stat) ){
while (my ($state, $state_ref) = each(%$year_ref) ){
while (my ($city, $city_ref) = each(%$state_ref) ){
foreach my $crime_ref (@$city_ref){
while (my ($crime, $crime_stat) = each (%$crime_ref) ){
my $sql = qq|INSERT INTO LOCATION (COUNTRY, STATE, DISTRICT, CITY, LOCALITY_NAME, PIN_CODE, SAFETY_RANKING) VALUES ('IN', '$state', '$city', '$city', 'city', '11111', 1)|;
my $stmt = $dbh->do($sql);
}
}
}
}
}
$dbh->disconnect();
代码运行正常并相应地插入数据。但每当我运行上面的脚本时,它就会填充数据。例如,我第一次运行它时会创建1000行,下次创建2000行,依此类推。有关如何防止它的任何建议,以便不在单个行中插入相同类型的数据。谢谢。
答案 0 :(得分:0)
您可能也对某些模块感兴趣 Class :: DBI,Rose :: DB,DBIx :: Class
看看这些......可能会让你的生活变得更轻松......祝你好运 Perl ORM database module comparison