DBI:如何为未知数据找到正确的数据类型?

发布于 2024-10-20 16:22:50 字数 1847 浏览 3 评论 0原文

对于这两种方法,您更喜欢:尝试找到正确的数据类型还是简单地使用始终 varchar?

# ...
use HTML::TableExtract;
my $te = HTML::TableExtract->new( headers => [ 'some headers', 'one', 'two' ], keep_headers => 1 );
$te->parse( $html_string );
die $te->tables if $te->tables != 1;
( my $grid ) = $te->tables;

use DBI;
my $dbh = DBI->connect( ... ) or die $DBI::errstr;
my $table = 'my_test_table';

my @rows = $grid->rows;
my $header_row = shift @rows;

#####  version 1  ####
use Data::Types qw(:all);
my @create_row;

for my $col ( 0 .. $#$header_row ) {
    my ( $count, $int, $float ) = ( 0, 0, 0 );
    my $longest = 0;
    for my $row ( @rows ) {
        $longest = length $row->[$col] if length $row->[$col] > $longest;
        $int++ if is_int( $row->[$col] );
        $float++ if is_float( $row->[$col] );
        $count++;
    }
    if ( $int == $count ) {
        $create_row[$col] = $dbh->quote( $header_row->[$col] ) . ' int';
    } 
    elsif ( $float == $count ) {
        $create_row[$col] = $dbh->quote( $header_row->[$col] ) . ' float';
    } 
    else {
        $create_row[$col] = $dbh->quote( $header_row->[$col] ) . " char($longest)";
    }
}

$sql = sprintf "CREATE TABLE $table ( %s )", 
join( ', ', @create_row );
$dbh->do( $sql );

$sql = sprintf "INSERT INTO $table ( %s ) VALUES( %s )", 
join( ',',  map { $dbh->quote( $_ ) } @$header_row ), join( ',', ('?') x @$header_row );
my $sth = $dbh->prepare( $sql );

####  version 2  ####
# always varchar

$sql = sprintf "CREATE TABLE $table ( %s )", 
join( ', ', map { "'$_' varchar(60)" } @$header_row );
$dbh->do( $sql );

$sql = sprintf "INSERT INTO $table ( %s ) VALUES( %s )", 
join( ',',  map { $dbh->quote( $_ ) } @$header_row ), join( ',', ('?') x @$header_row );
my $sth = $dbh->prepare( $sql );

With of these two approaches would you prefer: trying to find the right data-types or simply using always varchar?

# ...
use HTML::TableExtract;
my $te = HTML::TableExtract->new( headers => [ 'some headers', 'one', 'two' ], keep_headers => 1 );
$te->parse( $html_string );
die $te->tables if $te->tables != 1;
( my $grid ) = $te->tables;

use DBI;
my $dbh = DBI->connect( ... ) or die $DBI::errstr;
my $table = 'my_test_table';

my @rows = $grid->rows;
my $header_row = shift @rows;

#####  version 1  ####
use Data::Types qw(:all);
my @create_row;

for my $col ( 0 .. $#$header_row ) {
    my ( $count, $int, $float ) = ( 0, 0, 0 );
    my $longest = 0;
    for my $row ( @rows ) {
        $longest = length $row->[$col] if length $row->[$col] > $longest;
        $int++ if is_int( $row->[$col] );
        $float++ if is_float( $row->[$col] );
        $count++;
    }
    if ( $int == $count ) {
        $create_row[$col] = $dbh->quote( $header_row->[$col] ) . ' int';
    } 
    elsif ( $float == $count ) {
        $create_row[$col] = $dbh->quote( $header_row->[$col] ) . ' float';
    } 
    else {
        $create_row[$col] = $dbh->quote( $header_row->[$col] ) . " char($longest)";
    }
}

$sql = sprintf "CREATE TABLE $table ( %s )", 
join( ', ', @create_row );
$dbh->do( $sql );

$sql = sprintf "INSERT INTO $table ( %s ) VALUES( %s )", 
join( ',',  map { $dbh->quote( $_ ) } @$header_row ), join( ',', ('?') x @$header_row );
my $sth = $dbh->prepare( $sql );

####  version 2  ####
# always varchar

$sql = sprintf "CREATE TABLE $table ( %s )", 
join( ', ', map { "'$_' varchar(60)" } @$header_row );
$dbh->do( $sql );

$sql = sprintf "INSERT INTO $table ( %s ) VALUES( %s )", 
join( ',',  map { $dbh->quote( $_ ) } @$header_row ), join( ',', ('?') x @$header_row );
my $sth = $dbh->prepare( $sql );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱情眠于流年 2024-10-27 16:22:50

如果您正在处理的表不会更改,并且该列仅用于该单个表的数据,则可以安全地猜测似乎适合的数据类型(版本 1)。

但是,如果您计划向该列添加更多数据,那么您需要将所有内容保留为 varchar,以防将来出现一些不同类型的数据(版本 2)。

If the table you're processing will not change, and if the column will only be used for that single table's data, then it is safe to guess a data type that seems to fit (version 1).

However, if you plan to add any more data to that column then you'd need to keep everything as varchars in case there's some data of a different type in future (version 2).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文