mysql - DBD::MySQL:如何使 "LongReadLen"正常运行?

标签 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

最佳答案

像这样调整输出

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

关于mysql - DBD::MySQL:如何使 "LongReadLen"正常运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14504069/

相关文章:

php - 当此触发器查询直接在 mysql 中运行良好时,MySQL TRIGGER 不使用 $wpdb->query

mysql - 从 MySql DB 请求日期字段并保存在 Windows Store APP 类字段中

mysql - 查找包含值的记录

perl - Perl 中如何释放内存?

json - 如何在 Perl 中为 JSON 数组分配名称

sql - 当对DBI进行子类化时,Perl DBI将设置SQLite DB cache_size视为写操作

perl - 为什么 DBD::SQLite 不能通过我的 Perl CGI 脚本插入数据库?

PHP 文件下载在文件开头添加额外的空格

perl - Perl 中的 $dxyabc 和 ${dxyabc} 有什么区别?

sql-server - sp_tables 调用花费更多时间导致阻塞