DBD :: MySQL:如何将“LongReadLen”用于运行?

时间:2013-01-24 14:48:56

标签: mysql perl dbi truncate

如何更改此脚本以截断BLOB输出?

#!/usr/bin/env perl
use warnings;
use strict;
use utf8;
use 5.10.1;
use DBI;

my $user = 'username';
my $passwd = 'password';

my $db = 'information_schema';
my $dbh = DBI->connect( "DBI:mysql:dbname=$db", $user, $passwd, {
    RaiseError => 1,
    AutoCommit => 1,
} ) or die DBI->errstr;

$db = 'test_truncate';
$dbh->do( "DROP DATABASE IF EXISTS $db" );
$dbh->do( "CREATE DATABASE $db" );

$dbh = DBI->connect( "DBI:mysql:dbname=$db", $user, $passwd, {
    PrintError => 0,
    RaiseError => 1,
    AutoCommit => 1,
    mysql_enable_utf8 => 1,
} ) or die DBI->errstr;

$dbh->{LongReadLen} = 5;
$dbh->{LongTruncOk} = 1;

my $table = 'table_truncate';
$dbh->do( "CREATE TABLE IF NOT EXISTS $table ( Id INT, my_Blob BLOB )" );
my $sth = $dbh->prepare( "INSERT INTO $table ( Id, my_Blob ) VALUES ( ?, ? )" );

my $blob = '123456789' x 20;
$sth->execute( 1, $blob );

$sth = $dbh->prepare( "SELECT * FROM $table" );
$sth->execute();
while ( my $row = $sth->fetchrow_arrayref() ) {
    say for @$row;
}

输出:

1
123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789

1 个答案:

答案 0 :(得分:1)

像这样调整输出

while ( my $row = $sth->fetchrow_arrayref() ) {
    print substr($_,0,78)."\n" for @$row;
}