linux - perl - 打印 sql 行

标签 linux shell perl

我想打印我的 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/

相关文章:

python - python 中与 perl $conf{svnlook} log --revision $rev $repo 中此代码行的等效项是什么

c - SIGCONT 和 SIGHUP 命令发送到孤立的 linux 进程组

c++ - udp 套接字发送到隐式绑定(bind)

bash - 想要在 unix 中执行 tail 命令后退出

bash - 使用 FFmpeg 混契约(Contract)名音频和视频文件的脚本

regex - 使用 s///e 进行评估

linux - 防止同时打开 "rw"和 "r"中的文件

linux - NGINX 使用查询字符串重定向 URL

php - 不能用find和exec移动?

regex - 如何使用 Perl 替换在文件中具有转义序列的字符串?