简体   繁体   English

Perl DBI 不访问数据库

[英]Perl DBI without accessing the database

I'm creating a set of SQL INSERT statements for a database that doesn't exist yet, and I'm saving them to file.我正在为尚不存在的数据库创建一组 SQL INSERT语句,并将它们保存到文件中。

How can I use Perl's powerful DBI module to create those INSERT statements without accessing a specific database.如何在不访问特定数据库的情况下使用 Perl 强大的 DBI 模块来创建这些 INSERT 语句。 In particular, it looks like using the $dbh->quote() function requires that I instantiate $dbh with a connection to a database.特别是,看起来使用$dbh->quote() function 要求我实例化$dbh并连接到数据库。

Usually you would use DBI by specifying a database like so:通常你会通过像这样指定一个数据库来使用 DBI:

my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=127.0.0.1");

However, your database does not yet exist so you cannot connect to it.但是,您的数据库尚不存在,因此您无法连接到它。 You can use DBI without specifying a database like so:您可以在不指定数据库的情况下使用 DBI,如下所示:

my $dbh = DBI->connect("DBI:mysql:;host=127.0.0.1");

Unfortunately, the actual quote() behaviour isn't always a portable characteristic, so each driver will do them differently.不幸的是,实际的quote()行为并不总是可移植的特性,因此每个驱动程序都会以不同的方式执行它们。 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.). (有一个模块可以在没有连接的情况下执行此操作, DBIx::Abstract ,但它不是特别最新。)。

The quote() method is actually implemented by the corresponding driver class, in the DBD::* namespace. quote()方法实际上是由对应的驱动 class 实现的,在DBD::*命名空间中。 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.可能会尝试加载您需要的驱动程序并直接调用 function(参见http://search.cpan.org/~timb/DBI-1.616/lib/DBI/DBD.pm#Writing_DBD::Driver::db::引用),但这感觉很肮脏。

I'd still make a DBI connection, if only so that you get the right format of quoting.我仍然会建立DBI连接,如果只是为了让您获得正确的报价格式。 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.您实际上不需要向它发送任何语句,但是您确实知道引用格式对于您将使用的数据库是正确的。

From DBI::quote :来自DBI::quote

For most database types, at least those that conform to SQL standards, quote would return 'Don''t' (including the outer quotation marks).对于大多数数据库类型,至少那些符合 SQL 标准的数据库类型,quote 将返回 'Don''t'(包括外引号)。 For others it may return something like 'Don\'t'对于其他人,它可能会返回类似 'Don\'t'

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.也就是说, DBI::quote的行为因数据库而异,以独立于数据库的方式调用它是没有意义的。

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.与您正在编写的同一类型的数据库建立简单的连接,或者学习数据库的引用约定并自己实现quote方法。 See the DBI source for a reference implementation.请参阅DBI 源代码以获取参考实现。

You could use DBD::CSV or DBD::AnyData as a dummy database.您可以使用 DBD::CSV 或 DBD::AnyData 作为虚拟数据库。 SQLite is good for this purpose, too. SQLite 也适用于此目的。

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.在这里使用 SQLite 的一个隐藏优势是它是一个半真实的数据库,并且倾向于让您以与任何特定数据库分离的方式编写代码。

According to perl -MDBI -E 'say join(q{,},DBI->available_drivers);'根据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:在干净的 Debian chroot 中,仅安装了 DBI(包“libdbi-perl”),以下驱动程序立即可用:

DBM,ExampleP,File,Gofer,Proxy,Sponge

The minimum DBI connect statement that works for me is对我有用的最小 DBI 连接语句是

my $dbh=DBI->connect("DBI:DRIVER:");  # DRIVER is one of [DBM,File,ExampleP,Sponge,mysql,SQLite]

That is enough to use $dbh->quote() with no database whatsoever.这足以在没有任何数据库的情况下使用$dbh->quote()

DBM and File escape q{ ' } as q{ \' } (" mysql " style); DBM文件转义 q{ ' } as q{ \' } (" mysql " 样式);
ExampleP and Sponge escape: q{ ' } as q{ '' } (" SQLite " style). ExamplePSponge转义: q{ ' } as q{ '' } (“ SQLite ”样式)。

You can also use:您还可以使用:

DBD::_::db->quote()

To access the quote function without setting up a database handle.在不设置数据库句柄的情况下访问报价 function。 I don't believe it is specific to MySQL though.我不相信它是特定于 MySQL 的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM