简体   繁体   English

Perl DBI运行PSQL代码

[英]Perl dbi running psql code

I was wondering if anyone had any experience with perl dbi connecting to a postgres database. 我想知道是否有人对perl dbi连接到postgres数据库有任何经验。 So I can connect to my postgres database and execute select queries, but I can't execute psql queries. 因此,我可以连接到我的postgres数据库并执行选择查询,但不能执行psql查询。 Does anyone know if it is possible to do this in perl dbi? 有谁知道是否有可能在perl dbi中这样做? Right now I use this code to fetch a select query: 现在,我使用以下代码来获取选择查询:

my $SQL = $dbh->prepare("select*from pg_settings;);
    $SQL -> execute();
    while ( my ($settings_fetch) = $SQL->fetchrow_array() )
    {
        return $settings_fetch;
    }

with the module PG:DBI 与模块PG:DBI

But when I try to put \\x or \\pset format wrapped in the prepare statement it does not work. 但是,当我尝试将\\x\\pset格式包装在prepare语句中时,它不起作用。 It doesn't give any errors but the output won't wrap and i still get the same output. 它没有给出任何错误,但输出不会包装,我仍然得到相同的输出。 Now I am a little confused if it's because it does not execute the psql commands and wrap the fetched rows, or the selects run always the same way and the wrapping is only to show on the display and can't be fetched with dbi 现在,我有点困惑,因为它不执行psql命令并包装提取的行,或者选择始终以相同的方式运行,而包装仅显示在显示器上,而不能用dbi提取

The reason I want this is because I fetch the select arrays inside an array and output this to word, but when I got big select output with a lot of columns it does get messy in word tables, so I wanted the \\x variant inside my word document so that I don't get 10 columns horizontally but vertically. 我想要这个的原因是因为我获取了数组中的选择数组并将其输出到word,但是当我得到带有很多列的大select输出时,它的确在单词表中变得凌乱,所以我希望在我的\\x变量中Word文档,这样我就不会在水平方向垂直放置10列。

\\x and \\pset are formatting commands used by the psql client. \\x\\psetpsql客户端使用的格式化命令。 They are not used by the Postgres server. Postgres服务器不使用它们。

If you want to format the data output from your Perl program, you have to do the formatting in Perl. 如果要格式化从Perl程序输出的数据,则必须在Perl中进行格式化。

For example, rather than writing: 例如,而不是写:

foreach my $column (@$settings_fetch) {
    print $column;
}

You could: 你可以:

foreach my $column (@$settings_fetch) {
    print $column . "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM