2

所以我是 Perl 的新手,也是 SQLite 的新手。我有 SQL 经验,但是这种新的 Perl 语法让我有点吃惊。

所以我有一个问题,我试图使用 Perl 脚本从 IPtable 日志创建数据库,以解析表中的数据并向人们发送通知。该脚本还会向用户发送通知,但我认为这与此问题无关。

这是我收到的错误。

DBD::SQLite::db prepare failed: no such table: syslog_decom_notif at ./send_notification_syslog.pl line 251. 无法在 ./send_notification_syslog.pl 第 252 行对未定义值调用方法“执行”。

以下是我收到错误的代码:

2 sub select_contacts {
233         my @contact_info;
234         my $dbh = DBI->connect( DECOM_NOTIFICATION_DB ,"","");
235
236         my ( $where_clause, @exec_params ) = build_where_clause();
237
238         my $SQL = <<SQL;
239 select
240                 contact
241         ,       status
242         ,       contact_mngr
243         ,       hostname
244         ,       contact_type
245         ,       syslog_server
246 from
247         syslog_decom_notif
248 $where_clause
249 SQL
250         debug  ( __LINE__ . " Excuting SQL = \n[ $SQL ]\n" );
251         my $sth = $dbh->prepare( $SQL );
252         $sth->execute( @exec_params );
253         if ( $debug_mode ) {
254                 my @cols = @{$sth->{NAME}};
255                 print join '|', @cols;
256                 print "\n";
257         }
258         while (my @res = $sth->fetchrow) {
259                 for ( my $i=0; $i<@res; $i++ ) { $res[$i] = 'Null' if ! defined $res[$i]; }
260                 my $row = join '|', @res;
261                 debug "$row\n";
262                 push @contact_info, $row;
263         }
264         $sth->finish();
265         return @contact_info;
266 }

我四处寻找,似乎找不到任何可以真正帮助解决这个问题的东西。

我很欣赏任何和所有的想法。

此致

4

1 回答 1

5

syslog_decom_notif好吧,简单的事实是,正如它所说,DBI在您打开的数据库中找不到调用的表。失败后,prepare其他任何事情都不会起作用。

我想你的表名是正确的,所以我想知道DECOM_NOTIFICATION_DB. 这是从哪里来的?它可能是一个用 定义的字符串use constant吗?

connect调用 SQLite 时,DBI 需要一个看起来像dbi:SQLite:dbname=my_db_file. 如果文件不存在,它将被创建,所以也许你设置错误并且你正在使用一个空数据库?


更新

要查看您已连接到的数据库中的内容,请在DBI->connect调用后立即添加。它将打印连接数据库中的表列表。确保END终止符出现在行首,并且前后没有空格。

my $tables = $dbh->selectcol_arrayref(<<END);
SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name
END

print "$_\n" for @$tables;
于 2012-06-13T16:45:12.973 回答