当Perl的DBI在准备语句时遇到错误时,如何避免程序退出?
我正在制作一个脚本来遍历包含数据库上所有其他表名称的表。 当它解析每一行时,它会检查表是否为空,
select count(*) cnt from $table_name
某些表在架构中不再存在,如果我
select count(*)
直接在命令提示符中执行此操作,它会返回错误:
206: 指定的表(adm_rpt_rec)不在数据库中。
当我从 Perl 内部运行它时,它会将其附加到开头:
DBD::Informix::db 准备失败:SQL:-
当程序尝试准备此 SQL 语句时,如何避免程序退出?
I'm making a script that goes through a table that contains all the other table names on the database. As it parses each row, it checks to see if the table is empty by
select count(*) cnt from $table_name
Some tables don't exist in the schema anymore and if I do that
select count(*)
directly into the command prompt, it returns the error:
206: The specified table (adm_rpt_rec) is not in the database.
When I run it from inside Perl, it appends this to the beginning:
DBD::Informix::db prepare failed: SQL: -
How can I avoid the program quitting when it tries to prepare this SQL statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种选择是不使用 RaiseError => 1 构建$dbh时。 另一种是将准备包装在 eval 块中。
One option is not to use RaiseError => 1 when constructing $dbh. The other is to wrap the prepare in an eval block.
只需将可能失败的调用放在 eval 块中,如下所示:
尽管在本例中,由于您使用的是 Informix,所以您有一个更好的选择:系统目录表。 Informix 将这样的元数据保存在一组系统目录表中。 在这种情况下,您需要 systables:
这比针对表的
count(*)
更快、更安全。 系统目录表的完整文档可以在 IBM Informix SQL 指南(警告这是 PDF)。Just put the calls that may fail in an eval block like this:
Although, in this case, since you are using Informix, you have a much better option: the system catalog tables. Informix keeps metadata like this in a set of system catalog tables. In this case you want systables:
This is faster and safer than
count(*)
against the table. Full documentation of the system catalog tables can be found in IBM Informix Guide to SQL (warning this is a PDF).工作代码 - 假设您有一个“商店”数据库。
运行上面代码的输出。
这打印了错误 (
PrintError=>1
),但继续。 把1改成0就不会出现错误了。$tabname
和$num
声明中的括号至关重要 - 数组上下文与标量上下文。Working code - assuming you have a 'stores' database.
Output from running the code above.
This printed the error (
PrintError=>1
), but continued. Change the 1 to 0 and no error appears. The parentheses in the declarations of$tabname
and$num
are crucial - array context vs scalar context.