【问题标题】:To read database values in horizontal format with server names intact在服务器名称不变的情况下以水平格式读取数据库值
【发布时间】:2016-03-25 13:59:23
【问题描述】:

当前情景

我有一个数据库表,其中包含以下条目,它根据一天中每一小时的某些条件告诉服务器的状态是否正在使用。对于示例,我只给出了两台服务器及其状态两个小时。 :

ServerName  status     Time          
AAA         InUse      00:00:00   
BBB         NotInUse   00:00:00    
AAA         NotInUse   01:00:00    
BBB         InUse      01:00:00    

要求的结果

我正在寻找一个脚本,最好是 Shell 或 Perl 来读取这些数据库条目并在文本文件或 Web 中显示如下结果。这样服务器名称只会出现一次,并且只有状态和时间会水平更改。提前感谢您在这方面的建议和帮助。

  ServerName          Time/Status                  
               00:00:00      01:00:00          
   AAA         InUse         NotInUse
   BBB         NotInUse      InUse

到目前为止完成的工作

我正在使用如下 Perl/CGI 代码,并且数据显示在网页上,如当前场景所示:

my $sth = $dbh->prepare("SELECT Hostname ,Time_Stamp,Status FROM table WHERE Date ='$date' ");
$sth->execute() or die $DBI::errstr;

print $cgi->table({-border=>1});

# table headings are SQL column names

print "<tr><th>$sth->{NAME}->[0]</th><th>$sth->{NAME}->[1]</th><th>$sth->{NAME}->[2]</th><th>$sth->{NAME}->[3]</th></tr>";
while (my @row = $sth->fetchrow_array) {
    print "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td>\n";
}

【问题讨论】:

  • 我是新来的,我认为这是一个人们提问的地方,而不是寻找免费劳动力,但可能我错了
  • 您好,抱歉,@Alexandr Evstigneev,我将使用我通过 Perl 脚本尝试的现有代码更新问题
  • @user3510848:目前尚不清楚您的“数据库”是文本文件还是 MySQL 等关系数据库。请问是哪个?
  • @Borodin,它是一个 Mysql 数据库

标签: html sql perl shell cgi


【解决方案1】:

要将其打印到文本文件,您可以执行以下脚本。

对于本示例,我使用您的数据创建了一个测试数据库,以了解如何设置结果。

#!/usr/bin/perl
use strict;
use warnings;
use DBI;

my $dbh = DBI->connect(qq{DBI:CSV:});
$dbh->{'csv_tables'}->{'data'} = { 'file' => 'junk.txt',
        'col_names' => [qw/Hostname Status Time_Stamp/]};


my $sth = $dbh->prepare(qq{
                SELECT Hostname, Status, Time_Stamp
                FROM data
                });

$sth->execute() or die "Cannot execute: ", $sth->errstr();

my %data;
my %time;

while (my $href = $sth->fetchrow_hashref) {
    $data{ $href->{Hostname} }{ $href->{Time_Stamp} } = $href->{Status};
    $time{ $href->{Time_Stamp} }++;
}

my @times = sort keys %time;
my $fmt = "%-10s" . "%-10s" x @times . "\n";

printf $fmt, 'Server', @times;
print "-" x (10 * (1+@times)), "\n";

for my $server (sort keys %data) {
    printf $fmt, $server, @{ $data{$server} }{@times};  
}

输出是:

Server    00:00:00  01:00:00
------------------------------
AAA       InUse     NotInUse
BBB       NotInUse  InUse

更新:要获得(例如)每行 8 个时间列,您可以对代码进行以下更改。

use POSIX 'ceil';
my $col = 8;
my @times = sort keys %time;
@times = map{ [splice @times, 0, $col] } 1..ceil(@times/$col);
my $date = 'WHATEVER_DATE';

for my $aref (@times) {
    my $fmt = "%-10s" . "%-10s" x @$aref . "\n";

    print $date, "\n";
    printf $fmt, 'Server', @$aref;
    print "-" x (10 * (1+@$aref)), "\n";

    for my $server (sort keys %data) {
        printf $fmt, $server, @{ $data{$server} }{@$aref};  
    }

    print "\n\n";
}

我假设应该只有 24 个时间列 - 00:00:00 01:00:00 .. 23:00:00

【讨论】:

  • 这确实适用于相对较少数量的服务器和时间。但是当我有像 24 hrs 这样的时间时,直到页面结束后,时间在文本文件或网页中逐行出现。如下:
  • 00:01:00 02:00:00 03:00:00 04:0:00 05:00:00 06:00:00 07:00:00 08:00:00 9: 00:00 10:00:00 11:00:00 12:00:00 13:00:00 14:00:00 15:00:00
【解决方案2】:

$sth 是语句句柄,而不是结果。您应该先使用

读取数据
 $ary_ref = $dbh->selectall_arrayref($statement, {Slice=>{}});

然后在$ary_ref 中,您将获得对哈希引用数组的引用。每个哈希代表一行数据。只需遍历它们并根据需要打印内容。

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2013-05-04
    相关资源
    最近更新 更多