perl多线程实例(四个线程同时读MYSQL数据库获取记录)
发布时间:2020-12-16 00:09:27 所属栏目:大数据 来源:网络整理
导读:use threads;use DBI();# input:# $_[0]: fruitnamesub fetchFruit { # Connect to the database. my $dbh = DBI-connect("DBI:mysql:database=FruitDB;host=localhost","username","password",{'RaiseError' = 1}); # Now retrieve data from the table. my
use threads; use DBI(); # input: # $_[0]: fruitname sub fetchFruit { # Connect to the database. my $dbh = DBI->connect("DBI:mysql:database=FruitDB;host=localhost","username","password",{'RaiseError' => 1}); # Now retrieve data from the table. my $sth = $dbh->prepare("SELECT * FROM fruit where name = '".$_[0]."'"); $sth->execute(); while (my $ref = $sth->fetchrow_hashref()) { print "Found a row: name = $ref->{'name'},variety = $ref->{'variety'}n"; } $sth->finish(); # Disconnect from the database. $dbh->disconnect(); } my @arr = qw(APPLE ORANGE PEAR BANANA); my @threads; foreach (@arr) { push @threads,threads->new(&;fetchFruit,$_); } foreach (@threads) { $_->join(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |