Perl DBI无需访问数据库 [英] Perl DBI without accessing the database

查看:114
本文介绍了Perl DBI无需访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为尚不存在的数据库创建一组SQL INSERT 语句,并将它们保存到文件中。



如何使用Perl强大的DBI模块来创建这些INSERT语句,而无需访问特定的数据库。特别是,它看起来像使用 $ dbh-> quote()函数要求我实例化 $ dbh 不幸的是,实际的 quote() >行为并不总是一个便携的特性,所以每个驱动程序会做不同的。除非连接到驱动程序,否则在实践中不知道使用哪种引用格式。 (有一个模块可能没有连接,但是没有连接, DBIx :: Abstract ,但不是特别当前。)



quote()方法实际上是通过相应的驱动程序类在 DBD :: * 命名空间。您可以尝试加载所需的驱动程序,并直接调用该函数(请参阅 http://search.cpan.org/~timb/DBI-1.616/lib/DBI/DBD.pm#Writing_DBD::Driver: :db :: quote )但是这感觉很肮脏。



我仍然会创建一个 DBI 连接,如果只有这样才能获得正确的引用格式。您不需要实际发送任何语句,但是您知道您将使用的数据库的引用格式是正确的。


I'm creating a set of SQL INSERT statements for a database that doesn't exist yet, and I'm saving them to file.

How can I use Perl's powerful DBI module to create those INSERT statements without accessing a specific database. In particular, it looks like using the $dbh->quote() function requires that I instantiate $dbh with a connection to a database.

解决方案

Unfortunately, the actual quote() behaviour isn't always a portable characteristic, so each driver will do them differently. Unless you connect to a driver, you don't know which quoting format to use in practice. (There is one module that might do this without a connection, DBIx::Abstract, but it is not especially current.).

The quote() method is actually implemented by the corresponding driver class, in the DBD::* namespace. You might attempt to load the driver you need and call the function directly (see http://search.cpan.org/~timb/DBI-1.616/lib/DBI/DBD.pm#Writing_DBD::Driver::db::quote) but this feels grubby.

I'd still make a DBI connection, if only so that you get the right format of quoting. You don't need to actually send it any statements, but then you do know that the quoting format will be correct for the database you will use.

这篇关于Perl DBI无需访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆