Perl DBI 无需访问数据库

发布于 2024-11-19 20:28:04 字数 189 浏览 1 评论 0原文

我正在为尚不存在的数据库创建一组 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

喜你已久 2024-11-26 20:28:05

不幸的是,实际的 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 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.

故事↓在人 2024-11-26 20:28:05

来自 DBI::quote

对于大多数数据库类型,至少是那些符合 SQL 标准的数据库类型,quote 将返回“Don't”(包括外部引号)。对于其他人来说,它可能会返回类似“Don\'t”的内容

也就是说,DBI::quote 的行为因数据库而异,在数据库中调用它是没有意义的 -独立的方式。

与您正在编写的相同类型的数据库建立一个简单的连接,或者了解数据库的引用约定并自己实现一个 quote 方法。有关参考实现,请参阅 DBI 源

From DBI::quote:

For most database types, at least those that conform to SQL standards, quote would return 'Don''t' (including the outer quotation marks). For others it may return something like '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.

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.

黒涩兲箜 2024-11-26 20:28:05

通常,您可以通过指定数据库来使用 DBI,如下所示:

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

但是,您的数据库尚不存在,因此您无法连接到它。您可以使用 DBI 而无需指定数据库,如下所示:

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

Usually you would use DBI by specifying a database like so:

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:

my $dbh = DBI->connect("DBI:mysql:;host=127.0.0.1");
醉梦枕江山 2024-11-26 20:28:05

您可以使用 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.

衣神在巴黎 2024-11-26 20:28:05

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

DBM,ExampleP,File,Gofer,Proxy,Sponge

对我有用的最小 DBI 连接语句

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

足以使用 $dbh->quote() 没有任何数据库。

DBM文件 将 q{'} 转义为 q{\'} (“mysql”风格);
ExamplePSponge 转义: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:

DBM,ExampleP,File,Gofer,Proxy,Sponge

The minimum DBI connect statement that works for me is

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.

DBM and File escape q{'} as q{\'} ("mysql" style);
ExampleP and Sponge escape: q{'} as q{''} ("SQLite" style).

记忆消瘦 2024-11-26 20:28:05

您还可以使用:

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

无需设置数据库句柄即可访问报价功能。但我不认为它是 MySQL 特有的。

You can also use:

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

To access the quote function without setting up a database handle. I don't believe it is specific to MySQL though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文