1

I have to make a perl script populate a database in PostgreSQL without using DBI or any sort of database interface model. I am a beginner to scripting so naturally, I'v been stuck on this for quite a while. I only have this much so far.

    open my $pipe, '|-', "psql -d postgres -U postgres", @options or die;
    # NOT SURE WHAT TO DO AFTER THIS

    close $pipe;

edit 1: Now i'm trying to do this.

 for ($count = $iters; $count >= 1; $count--) {
    $randdecimal = rand();
    $pipe "INSERT INTO random_table (runid, random_number) VALUES ($runid, $randdecimal)";
 }

but it gives me a syntax error

4

1 回答 1

2

就像其他人说的那样,DBI比打印到管道要好得多

然而,有一个中途的房子。只需将所有 SQL 打印到 STDOUT,然后执行以下操作:

myscript.pl | psql -v ON_ERROR_STOP=1 --single-transaction -f -

这使您可以轻松检查脚本输出/将其发送到文件。psql 选项在第一个错误时停止,将所有内容包装在事务中并从 STDIN 读取。您可能也需要通常的 -h/-U 选项。

就个人而言,我倾向于打开两个终端,然后从 psql 提示符写入 .sql 文件,然后写入 \i 。我喜欢记录我运行的命令。

于 2013-04-13T17:32:38.763 回答