gpt4 book ai didi

linux - perl - 打印 sql 行

转载 作者:太空宇宙 更新时间:2023-11-04 09:09:48 31 4
gpt4 key购买 nike

我想打印我的 perl 脚本的所有 sql 结果,我已经连接到数据库,我想显示我的 sql 查询的结果:

MySQL("SELECT * FROM test");

# define subroutine to submit MySQL command
sub MySQL
{
#Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=database;host=ip",
"login", 'password',
{'RaiseError' => 1});

my $query = $_[0]; #assign argument to string

my $sth = $prepare($query); #prepare query

$sth->execute(); #execute query

while (my @row = $sth->fetchrow_array)
{
print "@row\n";
}
}

我有这个错误:

Global symbol "$prepare" requires explicit package name at test3.pl line 34.
syntax error at test3.pl line 34, near "$prepare("
Global symbol "$sth" requires explicit package name at test3.pl line 36.
Execution of test3.pl aborted due to compilation errors.

最佳答案

改变这一行:

my $sth = $prepare($query);

my $sth = $dbh->prepare($query);

并且不要忘记在 while 循环之后关闭 $sth$dbh 句柄:

$sth->finish;
$dbh->disconnect;

关于linux - perl - 打印 sql 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58694393/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com