perl中dbi句柄和返回操作实例

王朝perl·作者佚名  2008-05-18
宽屏版  字体: |||超大  

#!/my/gnu/bin/perl

# This is a test with stores big records in a blob

# Note that for the default test the mysql server should have been

# started with at least 'mysqld -O max_allowed_packet=200k'

#$host= shift || "";

$host="127.0.0.1";

$test_db="test";

$opt_user=$opt_password="";

use DBI;

$|= 1; # Autoflush

$table="test_big_record";

$rows=20; # Test of blobs up to ($rows-1)*10000+1 bytes

print "Connection to database $test_db\n";

$dbh = DBI->connect("DBI:mysql:$test_db:$host",$opt_user,$opt_password) || die "Can't connect: $DBI::errstr\n";

$dbh->do("drop table if exists $table");

print "Creating table $table\n";

($dbh->do("CREATE TABLE $table (

auto int(5) unsigned NOT NULL DEFAULT '0' auto_increment,

test mediumblob,

PRIMARY KEY (auto))")) or die $DBI::errstr;

print "Inserting $rows records\n";

for ($i=0 ; $i < $rows ; $i++)

{

$tmp= chr(65+$i) x ($i*10000+1);

$tmp= $dbh->quote($tmp);

$dbh->do("insert into $table (test) values ($tmp)") or die $DBI::errstr;

}

print "Testing records\n";

$sth=$dbh->prepare("select * from $table") or die $dbh->errstr;

$sth->execute() or die $sth->errstr;

$i=0;

while (($row = $sth->fetchrow_arrayref))

{

print $row->[0]," ",length($row->[1]),"\n";

die "Record $i had wrong data in blob" if ($row->[1] ne (chr(65+$i)) x ($i*10000+1));

$i++;

}

die "Didn't get all rows from server" if ($i != $rows);

$dbh->do("drop table $table") or die $DBI::errstr;

print "Test ok\n";

exit 0;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有