为什么DBI隐式将整数更改为字符串? [英] Why does DBI implicitly change integers to strings?

查看:45
本文介绍了为什么DBI隐式将整数更改为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的MySQL表.

I have a MySQL table with following structure.

alid       bigint(20),
ndip       varchar(20),
ndregion   varchar(20),
occ_num    int(3),
Delta_Flag int(1)

从表中选择数据后,我将所有数据都加引号并作为字符串值.

After selecting data from the table, I am getting all the data quoted and as a string value.

#!/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
    }
}

Database.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将所有整数隐式转换为字符串?

Why is DBI implicitly converting all integers to strings?

推荐答案

正如@choroba在他的回答中指出的那样,不是DBI在处理数据.只是通过驱动程序模块(在您的情况下为DBD :: mysql)返回的内容.

As @choroba notes in his answer, it's not the DBI that's doing anything with the data. It's just passing through what the driver module (DBD::mysql in your case) returned.

常规接口规则&它说的DBI文档的警告部分:

大多数数据作为字符串返回到Perl脚本. (将空值作为undef返回.)这允许在不损失精度的情况下处理任意精度的数值数据.请注意,将字符串用作数字时,Perl可能不会保持相同的准确性.

Most data is returned to the Perl script as strings. (Null values are returned as undef.) This allows arbitrary precision numeric data to be handled without loss of accuracy. Beware that Perl may not preserve the same accuracy when the string is used as a number.

我曾写道,在将perl配置为支持64位整数之前是很常见的,而长双精度浮点数类型并不常见.这些天,我建议驱动程序以最自然"的Perl类型返回值,而不会丢失数据.

I wrote that back in the days before it was common to configure perl to support 64-bit integers, and long-double floating point types were unusual. These days I recommend that drivers return values in the most 'natural' Perl type that doesn't risk data loss.

对于某些可能难以实现的驱动程序,尤其是那些支持从单个句柄返回多个具有不同列数的结果集的驱动程序,如DBD :: mysql一样.

For some drivers that can be tricky to implement, especially those that support returning multiple result sets, with different numbers of columns, from a single handle, as DBD::mysql does.

我略过了 DBD :: mysql文档,但没有看到任何提及这个主题,所以我看了相关代码可以看到当前的DBD :: mysql 将数字作为数字返回.在更改日志中,还有很多对该区域的最新更改的引用.

I skimmed the DBD::mysql docs but didn't see any mention of this topic, so I looked at the relevant code where I can see that the current DBD::mysql is returning numbers as numbers. There's also lots of references to recent changes in this area in the Change log.

也许您正在使用旧版本的DBD :: mysql并应进行升级.

Perhaps you're using an old version of DBD::mysql and should upgrade.

这篇关于为什么DBI隐式将整数更改为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆