【问题标题】:Intersystems Cache SQL shell: How to spool the output of a query to a file?Intersystems Cache SQL shell:如何将查询的输出假脱机到文件中?
【发布时间】:2011-03-24 19:15:04
【问题描述】:

我正在尝试在 UNIX 中创建一个脚本来查询缓存数据库。我可以获得输出,但如何将输出写入文件> 我使用了以下程序:

cache << EOF
DO \$SYSTEM.SQL.Shell()
SELECTMODE DISPLAY
SELECT * from .....
GO
EXIT
EOF

【问题讨论】:

    标签: intersystems-cache


    【解决方案1】:

    如果您将它用于报告/网页/等,我会查看 Intersystems 为 Perl、Python、Ruby 等提供的内置模块。这些应该为您提供更清晰的数据库接口。

    也就是说,我出于监控目的进行了一些不使用语言 API 的外部调用,因为我试图从数据库中获取信息(通常是内部信息)。在这些情况下,我使用 Perl。我使用 Perl 是因为这样做我需要解析出菜单和其他我不想要的东西(你不会通过 API 获得)。以下是查看缓存中存在的所有用户的简单示例。

    #!/usr/bin/perl
    
    use strict;
    
    my $username = 'user';
    my $password = 'password'; 
    
    my $val = `csession $instance -U %SYS << done
    $username
    $password
    
    d ^SECURITY
    1
    3
    *
    *
    
    
    
    h
    done`;
    
    my @users = split(/\n/, $val);
    my $in_users = 0;
    my $output = '';
    foreach (@users){
         if($in_users){
        chomp($_);
        if($_ eq ''){
            #no longer listing users
            $in_users = 0;
        } else {
                $output .= "$_\n";
            } 
      }
      $in_users = 1 if($_ =~ m/^\-\-\-\-/);  # I started a the user list 
    }
    
    print $output; # this prints out to the console
    
    #This prints to a file.
    open(OUTFILE, ">outfile.txt");
    print OUTFILE $output;
    

    查看以下有关 Perl 和 Python API 的链接 http://docs.intersystems.com/cache20102/csp/docbook/DocBook.UI.Page.cls?KEY=GBPLhttp://docs.intersystems.com/cache20102/csp/docbook/DocBook.UI.Page.cls?KEY=GBPY

    【讨论】:

      【解决方案2】:

      这有点骇人听闻,但以下应该可以工作:

      echo -e "username\npassword\nDO \$SYSTEM.SQL.Shell()\nfoo\nbar\nH\n" | cache > output
      

      您将在 Caché 中输入的每个命令后跟一个“\n”以表示换行。然后将其通过管道传输到新会话中,并将其输出写入文件。

      【讨论】:

        猜你喜欢
        • 2018-09-26
        • 2021-10-11
        • 2020-08-06
        • 2013-05-19
        • 1970-01-01
        • 2014-09-16
        • 2021-09-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多