Perl DBI 无需访问数据库
我正在为尚不存在的数据库创建一组 SQL INSERT
语句,并将它们保存到文件中。
如何使用 Perl 强大的 DBI 模块来创建这些 INSERT 语句而不访问特定的数据库。特别是,使用 $dbh->quote()
函数需要我实例化 $dbh
并连接到数据库。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不幸的是,实际的
quote()
行为并不总是可移植的特性,因此每个驱动程序都会以不同的方式执行它们。除非您连接到驱动程序,否则您不知道在实践中使用哪种引用格式。 (有一个模块可以在没有连接的情况下执行此操作,DBIx::Abstract
,但它不是特别最新。)。quote()
方法实际上是由DBD::*
命名空间中相应的驱动程序类实现的。您可能尝试加载所需的驱动程序并直接调用该函数(请参阅http://search.cpan.org/~timb/DBI-1.616/lib/DBI/DBD.pm#Writing_DBD::Driver::db::quote)但这感觉很脏。我仍然会建立一个
DBI
连接,只要您能获得正确的引用格式即可。您实际上不需要向其发送任何语句,但您确实知道引用格式对于您将使用的数据库来说是正确的。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 theDBD::*
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.来自
DBI::quote
:也就是说,
DBI::quote
的行为因数据库而异,在数据库中调用它是没有意义的 -独立的方式。与您正在编写的相同类型的数据库建立一个简单的连接,或者了解数据库的引用约定并自己实现一个
quote
方法。有关参考实现,请参阅 DBI 源。From
DBI::quote
:That is, the behavior of
DBI::quote
varies from database to database, and it doesn't make sense to call it in a database-independent way.Make a trivial connection to a database of the same type you are writing for, or learn your database's quoting conventions and implement a
quote
method yourself. See the DBI source for a reference implementation.通常,您可以通过指定数据库来使用 DBI,如下所示:
但是,您的数据库尚不存在,因此您无法连接到它。您可以使用 DBI 而无需指定数据库,如下所示:
Usually you would use DBI by specifying a database like so:
However, your database does not yet exist so you cannot connect to it. You can use DBI without specifying a database like so:
您可以使用 DBD::CSV 或 DBD::AnyData 作为虚拟数据库。 SQLite 也非常适合此目的。
在这里使用 SQLite 的一个隐藏优势是它是一个半真实数据库,并且往往会让您以与任何特定数据库解耦的方式编写代码。
You could use DBD::CSV or DBD::AnyData as a dummy database. SQLite is good for this purpose, too.
A hidden advantage of using SQLite here is that it's a semi-real database, and will tend to make you write code in a way that's decoupled from any specific database.
根据perl -MDBI -E 'say join(q{,},DBI->available_drivers);'
在仅安装了 DBI(软件包“libdbi-perl”)的干净 Debian chroot 中,可以立即使用以下驱动程序:
对我有用的最小 DBI 连接语句
足以使用
$dbh->quote()
没有任何数据库。DBM 和 文件 将 q{'} 转义为 q{\'} (“mysql”风格);
ExampleP 和 Sponge 转义:q{'} as q{''} ("SQLite< /em>”风格)。
According to
perl -MDBI -E 'say join(q{,},DBI->available_drivers);'
in clean Debian chroot with only DBI (package "libdbi-perl") installed the following drivers are available right away:
The minimum DBI connect statement that works for me is
That is enough to use
$dbh->quote()
with no database whatsoever.DBM and File escape q{'} as q{\'} ("mysql" style);
ExampleP and Sponge escape: q{'} as q{''} ("SQLite" style).
您还可以使用:
无需设置数据库句柄即可访问报价功能。但我不认为它是 MySQL 特有的。
You can also use:
To access the quote function without setting up a database handle. I don't believe it is specific to MySQL though.