【发布时间】:2017-04-09 13:44:13
【问题描述】:
我有一个具有以下结构的 MySQL 表。
alid bigint(20),
ndip varchar(20),
ndregion varchar(20),
occ_num int(3),
Delta_Flag int(1)
从表中选择数据后,我得到所有引用的数据并作为字符串值。
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use FindBin;
use lib $FindBin::Bin;
use Database;
my $pwd = $FindBin::Bin;
my $db = Database->new( 'mysql', "$pwd/config.ini" );
my $db1 = Database->new( 'mysql', "$pwd/config2.ini" );
my @tables = qw( AutoTT_AlarmStatus_Major1 );
for my $table ( @tables ) {
my $query_select = "SELECT alid, ndip, ndregion, occ_num, Delta_Flag FROM $table LIMIT 1";
my $result = $db->db_get_results( $query_select );
print Dumper( $result );
for my $item ( @{$result} ) {
# Here I want to prepare, bind and insert this data
# into other table with same structure
}
}
数据库.pm
sub db_get_results {
my $self = shift;
my $qry = shift;
my $sth = $self->{dbh}->prepare( $qry );
$sth->execute();
my @return = ();
while ( my @line = $sth->fetchrow_array ) {
push @return, \@line;
}
return \@return;
}
输出:
$VAR1 = [
[
'1788353',
'10.34.38.12',
'North Central',
'1',
'1'
]
];
为什么DBI 隐式将所有整数转换为字符串?
【问题讨论】:
-
你知道它在 Perl 中没有任何区别吗?
-
是的,不是在 Perl 中,但如果我想将此数据插入到具有相同结构的其他表中,那么这是一个问题。我需要明确地进行转换吗?
-
“不在 Perl 中”是什么意思?您可以使用数据按原样使用 Perl 创建新记录。如果您没有使用
Data::Dumper查看数据,那么您会发现很难发现您使用的是字符串还是整数。你为什么不试试呢? -
@Borodin,虽然在 Perl 中字符串与数字的问题通常无关紧要,但有时确实如此。序列化为 JSON 是一个常见的例子。
-
你使用的是什么版本的 DBD::mysql?