0

如何更改此脚本以截断 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
4

1 回答 1

1

像这样调整输出

while ( my $row = $sth->fetchrow_arrayref() ) {
    print substr($_,0,78)."\n" for @$row;
}
于 2013-01-24T14:59:05.950 回答