简体   繁体   English

DBI和DBD有什么区别?

[英]What is the difference between DBI and DBD?

Can someone please shed some light on what exactly is DBI and DBD? 有人可以了解一下DBI和DBD究竟是什么? When should either one be used and the benefits of using one over the other. 什么时候应该使用一个和使用一个优于另一个的好处。

DBI is database access library, whereas DBDs are "drivers" which are used by DBI to access particular database (eg. there is one DBD for MySQL, another one for PostgreSQL etc). DBI是数据库访问库,而DBD是DBI用于访问特定数据库的“驱动程序”(例如,有一个用于MySQL的DBD,另一个用于PostgreSQL等)。 You should use DBI rather than DBDs directly. 您应该直接使用DBI而不是DBD。

From the DBI docs : 来自DBI文档

             |<- Scope of DBI ->|
                  .-.   .--------------.   .-------------.
  .-------.       | |---| XYZ Driver   |---| XYZ Engine  |
  | Perl  |       | |   `--------------'   `-------------'
  | script|  |A|  |D|   .--------------.   .-------------.
  | using |--|P|--|B|---|Oracle Driver |---|Oracle Engine|
  | DBI   |  |I|  |I|   `--------------'   `-------------'
  | API   |       | |...
  |methods|       | |... Other drivers
  `-------'       | |...
                  `-'

The boxes labeled XYZ driver and Oracle driver are DBD modules. 标有XYZ driverOracle driver的框是DBD模块。

So your code talks to DBI. 所以你的代码与DBI对话。 DBI talks to the appropriate DBD module for your database. DBI与您的数据库的相应DBD模块进行对话。 The DBD module talks to your database. DBD模块与您的数据库通信。 This results in a single, consistent interface to different databases. 这导致与不同数据库的单一,一致的接口。

DBI is the interface. DBI是界面。 DBD are the implementations of that interface. DBD是该接口的实现。

DBI stands for database interface . DBI代表数据库接口 DBD stands for database driver . DBD代表数据库驱动程序

As a programmer you should always use the interface (DBI). 作为程序员,您应该始终使用接口(DBI)。 The interface, in turn, uses the driver. 反过来,接口使用驱动程序。 The reason to use DBI instead of using DBD directly is that it provides a consistent abstraction layer for working with databases. 使用DBI而不是直接使用DBD的原因是它为处理数据库提供了一致的抽象层。 There are many DBD modules but you only need to learn one interface. 许多DBD模块,但您只需要学习一个接口。 Additionally, this makes it relatively easy to change the database your application uses by simply changing the driver. 此外,通过简单地更改驱动程序,可以相对轻松地更改应用程序使用的数据库。 The interface is the same. 界面是一样的。 (The query syntax might be a little different.) (查询语法可能略有不同。)

Use them together. 一起使用它们。 For example, with MySQL : 例如,使用MySQL

use DBI;

$dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
$dbh = DBI->connect($dsn, $user, $password);

$sth = $dbh->prepare("SELECT * FROM foo WHERE bla");
$sth->execute;

If instead you're talking to an Oracle database, you may be able to get away with changing only the $data_source argument to DBI::connect : 如果您正在与Oracle数据库交谈,那么您可以通过仅将$data_source参数更改为DBI::connect

$dbh = DBI->connect("dbi:Oracle:host=$host;sid=$sid", $user, $password);

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

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