简体   繁体   English

我应该使用哪个Perl数据库接口?

[英]Which Perl database interface should I use?

Is CPAN DBI the best database interface to use in Perl for general database use? CPAN DBI是Perl中用于一般数据库使用的最佳数据库接口吗? Are there some better options? 有更好的选择吗?

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. 如果你只是在寻找低级数据库访问 - 你可以提供任何SQL字符串(可选择使用占位符和绑定值)并运行查询并返回结果 - 然后是, DBI是你最好的选择,到目前为止。

If you want a higher-level interface (ie, one that requires little or no use of raw SQL in your code) then there are several ORMs ( object-relational mappers ) available for Perl. 如果你想要一个更高级别的接口(即在你的代码中很少或不需要使用原始SQL的接口),那么Perl可以使用几个ORM( 对象关系映射器 )。 Check out the ORM page at the Perl Foundation's Perl 5 wiki for more information and links. 有关更多信息和链接,请查看Perl Foundation的Perl 5 wiki上的ORM页面。 (If you want help choosing among them or have specific questions, you could narrow the focus of this question or perhaps post another one.) (如果您需要帮助选择其中或有特定问题,您可以缩小此问题的重点或者可能发布另一个问题。)

DBI is the "low level" interface between Perl and an DBMS. DBI是Perl和DBMS之间的“低级”接口。 It's pretty much the only realistic choice for doing that. 这几乎是唯一可行的选择。 Comparable to JDBC in Java. 与Java中的JDBC相当。 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. 对于Perl和数据库之间的低级接口,你会疯狂(或者有一个非常具体的用例)来选择除DBI以外的任何东西。

On top of DBI, there are various object/relational mappers which make working with a database much easier and cleaner. 在DBI之上,有各种对象/关系映射器,这使得使用数据库变得更加容易和清晰。

Some of the common/more popular ones are 一些常见的/更受欢迎的是

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 . 如果您选择将纯DBI用于不需要ORM的任务,我强烈建议您查看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 . 它不是一个替代品,而是一个在DBI之上设计得非常好的API,可以使简单的事情变得简单而复杂,而不会失去任何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)? 你有没有发现你必须在DBI文档中查找看似简单的东西,比如将查询结果作为散列的arrayref(行)(列及其值)? With DBIx::Simple this is straightforward: 使用DBIx::Simple这很简单:

# 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. 此外,与SQL :: Abstract的集成使得简单的查询变得轻而易举。 It use it in all of my code where I would have used DBI before, and I'm not looking back. 它在我之前使用过DBI所有代码中都使用它,我不会回头看。

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. 值得指出的是,绝大多数“高级”接口(如SQL :: Abstract)和(DBIx :: Simple)在实际执行查询时都使用DBI本身。 DBI is pretty much the accepted standard method for database connection in Perl. DBI几乎是Perl中数据库连接的公认标准方法。

If you want to work with objects (with introspection!), take a look at Fey::ORM which implements ORM based on Moose. 如果你想使用对象(有内省!),请看看基于Moose实现ORM的Fey :: ORM。 It's also has very SQL like syntax so it fits my RDBMS-based brain a bit better than some of other ORM frameworks. 它也具有非常类似SQL的语法,因此它比其他一些ORM框架更适合我的基于RDBMS的大脑。

看看Class :: DBI也是如此。

In my opinion, DBI is a really good choice. 在我看来,DBI是一个非常好的选择。 I've used DBD::mysql actively and found it to be a really good solution. 我积极地使用了DBD :: mysql,发现它是一个非常好的解决方案。

We use the DBI module in all of our projects as well. 我们在所有项目中也使用DBI模块。 Many times we build a custom package on top of it for the specific application but underneath that is the core DBI module. 很多时候,我们为特定的应用程序在它上面构建一个自定义包,但在其下面是核心DBI模块。 And often it is just easier to use the DBI module functions directly. 通常,直接使用DBI模块功能会更容易。

DBI is great, but the quality of the DBD modules can vary. DBI很棒,但DBD模块的质量可能会有所不同。 I was bitten by a 'feature' in one of the versions of DBD:pg. 我被其中一个版本的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. 按照惯例 - Caveat programmor。

DBI rocks! DBI摇滚! but for a proper fully-featured ORM that is fast go for DBIx::Class all the time. 但是对于一个适当的全功能ORM来说,DBIx :: Class一直很快。

基本上你应该习惯只使用DBI。

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

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