DBI is great, but the quality of the DBD modules can vary. I was bitten by a 'feature' in one of the versions of DBD:pg. It liked to load the full data of your result into memory, rather than interate over it with cursors.
We use the DBI module in all of our projects as well. Many times we build a custom package on top of it for the specific application but underneath that is the core DBI module. And often it is just easier to use the DBI module functions directly.
It's worth pointing out that the vast majority of the "higher-level" interfaces (like SQL::Abstract) and (DBIx::Simple) use DBI itself when actually performing the queries. DBI is pretty much the accepted standard method for database connection in Perl.
If you want to work with objects (with introspection!), take a look at Fey::ORM which implements ORM based on Moose. It's also has very SQL like syntax so it fits my RDBMS-based brain a bit better than some of other ORM frameworks.
# DBI
my $rows = $dbh->selectall_arrayref($sql, { Slice => {} });
# tell it we want "hashes" (yuck!) ^^^^
# DBIx::Simple
my $rows = $db->query($sql)->hashes; # does the same as the above code underneath!
If you chose to use plain DBI for a task that doesn't need an ORM, I strongly suggest you take a look at DBIx::Simple.
It's not a replacement, but a very well designed API on top of DBI that makes simple things simple and complex things possible, without losing any of the flexibilty of DBI.
Did you ever found you had to look up apparently simple things in the DBI documentation, like getting the results of a query as an arrayref (rows) of hashes (columns and their values)? With DBIx::Simple this is straightforward:
# DBI
my $rows = $dbh->selectall_arrayref($sql, { Slice => {} });
# tell it we want "hashes" (yuck!) ^^^^
# DBIx::Simple
my $rows = $db->query($sql)->hashes; # does the same as the above code underneath!
Take a look at the examples for more. Also, the integration with SQL::Abstract makes simple queries a breeze. It use it in all of my code where I would have used DBI before, and I'm not looking back.
DBI is the "low level" interface between Perl and an DBMS. It's pretty much the only realistic choice for doing that. Comparable to JDBC in Java. You would be crazy (or have a very specific use case) to pick anything other than DBI for you low level interface between Perl and a database.
On top of DBI, there are various object/relational mappers which make working with a database much easier and cleaner.
If you're just looking for low-level database access—you feed it any SQL string (optionally with place-holders and bind values) and it runs your query and gives you back the results—then yes, DBI is your best bet, by far.
If you want a higher-level interface (i.e., one that requires little or no use of raw SQL in your code) then there are several ORMs (object-relational mappers) available for Perl. Check out the ORM page at the Perl Foundation's Perl 5 wiki for more information and links. (If you want help choosing among them or have specific questions, you could narrow the focus of this question or perhaps post another one.)
发布评论
评论(11)
DBI 很棒,但 DBD 模块的质量可能会有所不同。 我被 DBD:pg 的一个版本中的一个“功能”所困扰。 它喜欢将结果的完整数据加载到内存中,而不是使用游标对其进行交互。
像往常一样 - 警告程序员。
DBI is great, but the quality of the DBD modules can vary. I was bitten by a 'feature' in one of the versions of DBD:pg. It liked to load the full data of your result into memory, rather than interate over it with cursors.
As per usual - Caveat programmor.
DBI 震撼! 但对于一个功能齐全、速度快的 ORM,始终选择 DBIx::Class。
DBI rocks! but for a proper fully-featured ORM that is fast go for DBIx::Class all the time.
基本上您首先应该习惯只使用 DBI。
Basically you should be used to using only DBI firstly.
我们也在所有项目中使用 DBI 模块。 很多时候,我们在其之上为特定应用程序构建自定义包,但其下方是核心 DBI 模块。 通常,直接使用 DBI 模块功能会更容易。
We use the DBI module in all of our projects as well. Many times we build a custom package on top of it for the specific application but underneath that is the core DBI module. And often it is just easier to use the DBI module functions directly.
在我看来,DBI确实是一个不错的选择。 我积极使用 DBD::mysql 并发现它是一个非常好的解决方案。
In my opinion, DBI is a really good choice. I've used DBD::mysql actively and found it to be a really good solution.
看看 Class::DBI 作为出色地。
Have a look at Class::DBI as well.
值得指出的是,绝大多数“高级”接口(例如 SQL::Abstract)和(DBIx::Simple)在实际执行查询时都使用 DBI 本身。 DBI 几乎是 Perl 中公认的数据库连接标准方法。
It's worth pointing out that the vast majority of the "higher-level" interfaces (like SQL::Abstract) and (DBIx::Simple) use DBI itself when actually performing the queries. DBI is pretty much the accepted standard method for database connection in Perl.
如果您想使用对象(进行内省!),请查看 Fey::ORM,它基于 Moose 实现了 ORM。 它还具有非常类似于 SQL 的语法,因此它比其他一些 ORM 框架更适合我基于 RDBMS 的大脑。
If you want to work with objects (with introspection!), take a look at Fey::ORM which implements ORM based on Moose. It's also has very SQL like syntax so it fits my RDBMS-based brain a bit better than some of other ORM frameworks.
如果您选择使用普通的
DBI
来完成不需要 ORM 的任务,我强烈建议您查看 DBIx::Simple。
它不是替代品,而是在
DBI
之上设计良好的 API使简单的事情变得简单,使复杂的事情成为可能,而无需
失去
DBI
的任何灵活性。您是否发现必须在
DBI
中查找看似简单的内容文档,例如将查询结果作为 arrayref (行)获取
哈希值(列及其值)?
使用
DBIx::Simple
这很简单:看看 示例了解更多信息。 此外,与 SQL::Abstract 的集成使简单的查询变得轻而易举。 它在我之前使用
DBI
的所有代码中都使用了它,并且我不会回头。
If you chose to use plain
DBI
for a task that doesn't need an ORM, Istrongly suggest you take a look at DBIx::Simple.
It's not a replacement, but a very well designed API on top of
DBI
that makes simple things simple and complex things possible, without
losing any of the flexibilty of
DBI
.Did you ever found you had to look up apparently simple things in the
DBI
documentation, like getting the results of a query as an arrayref (rows)
of hashes (columns and their values)?
With
DBIx::Simple
this is straightforward:Take a look at the examples for more. Also, the integration with SQL::Abstract makes simple queries a breeze. It use it in all of my code where I would have used
DBI
before, andI'm not looking back.
DBI 是 Perl 和 DBMS 之间的“低级”接口。 这几乎是这样做的唯一现实选择。 相当于Java中的JDBC。 如果你为 Perl 和数据库之间的低级接口选择 DBI 以外的任何东西,你会很疯狂(或者有一个非常具体的用例)。
在 DBI 之上,还有各种对象/关系映射器,它们使数据库的使用变得更加容易和清晰。
一些常见/更流行的有
DBI is the "low level" interface between Perl and an DBMS. It's pretty much the only realistic choice for doing that. Comparable to JDBC in Java. You would be crazy (or have a very specific use case) to pick anything other than DBI for you low level interface between Perl and a database.
On top of DBI, there are various object/relational mappers which make working with a database much easier and cleaner.
Some of the common/more popular ones are
如果您只是寻找低级数据库访问 - 您向其提供任何 SQL 字符串(可以选择使用占位符和绑定值),它会运行您的查询并返回结果 - 那么是的,DBI 是迄今为止您最好的选择。
如果您想要一个更高级别的接口(即,在代码中很少或不需要使用原始 SQL 的接口),那么有几个 ORM(对象关系映射器)可用于 Perl。 查看 Perl 基金会 Perl 5 wiki 上的 ORM 页面,了解更多信息和链接。 (如果您需要帮助在其中进行选择或有具体问题,您可以缩小此问题的焦点,或者发布另一个问题。)
If you're just looking for low-level database access—you feed it any SQL string (optionally with place-holders and bind values) and it runs your query and gives you back the results—then yes, DBI is your best bet, by far.
If you want a higher-level interface (i.e., one that requires little or no use of raw SQL in your code) then there are several ORMs (object-relational mappers) available for Perl. Check out the ORM page at the Perl Foundation's Perl 5 wiki for more information and links. (If you want help choosing among them or have specific questions, you could narrow the focus of this question or perhaps post another one.)