mdb 转db

介绍(Intro)

Recently I had to move an existing project from using PEAR::DB to PEAR::MDB2 - the new database abstraction layer. I took notes on the parts of the code I needed to change, I hope they can benefit someone who's doing the same. Many thanks go to Lukas Smith, the lead developer, he was always responding very fast to my reports and questions in the PEAR mailing list.

最近,我不得不将现有项目从使用PEAR :: DB迁移到PEAR :: MDB2-新的数据库抽象层。 我记下了需要更改的代码部分,希望它们可以使这样做的人受益。 非常感谢首席开发人员Lukas Smith ,他一直对PEAR邮件列表中的报告和问题做出非常快速的响应。

One thing to notice in MDB2 is that it tries not to do any unnecessary work and does many things only on demand. For example when you create an object, that doesn't mean that a connection is established. It is established only when you make the first real database access, a SELECT for example.

在MDB2中要注意的一件事是,它试图不做任何不必要的工作,并且仅在需要时才做很多事情。 例如,当您创建对象时,这并不意味着已建立连接。 仅当您首次进行真正的数据库访问(例如SELECT时,才会建立该数据库。

I assume you have an idea of PEAR::DB, since this posting illustrates a DB-to-MDB2 endeavour, but even if you don't, I hope the posting will still be useful as an intro to DB and MDB2.

我假设您有一个PEAR :: DB的想法,因为此发布说明了从DB到MDB2的工作,但是即使您没有,我希望该发布对DB和MDB2的介绍还是有用的。

包括库 (Including the libs)

So first off, including the libs (I assume you have PEAR on your machine).

所以首先,包括库(我假设您的计算机上装有PEAR)。

require_once 'DB.php';
require_once 'MDB2.php';

One thing to note here is that installing MDB2 doesn't install any of the database wrappers. So if you use MySQL for example, you'd need to install it separately: pear install MDB2_Driver_mysql-beta in addition to pear install MDB2-beta

这里要注意的一件事是,安装MDB2不会安装任何数据库包装程序。 因此,例如,如果您使用MySQL,则需要单独安装它:除pear install MDB2-beta外, pear install MDB2-beta pear install MDB2_Driver_mysql-beta pear install MDB2-beta

MDB2 is now a stable release! So you can now remove the "-beta" monkier when installing the packages.

MDB2现在是一个稳定的发行版! 因此,您现在可以在安装软件包时删除“ -beta ” Monkier。

DSN (DSN)

Next - the DSN string. It's the same for MDB2 as for DB.

下一个-DSN字符串。 MDB2与DB相同。

$dsn = 'mysql://root@localhost/db2mdb2';

BTW, MDB2 can also accept an array of all the connection details, as opposed to a DSN string. And so does DB (Thanks for the clarification, Justin!)

顺便说一句,与DSN字符串相反,MDB2也可以接受所有连接详细信息的数组。 DB也是如此(感谢您的澄清,Justin!)

创建实例 (Creating instances)

$db =& DB::connect($dsn);
$mdb2 =& MDB2::factory($dsn);

MDB2 provides a factory method to create an instance. At this time no database connection is yet established. MDB2 also provides a singleton() method to create an instance.

MDB2提供了一种创建实例的工厂方法。 目前,尚未建立数据库连接。 MDB2还提供了singleton()方法来创建实例。

提取模式 (Fetchmode)

It is the same in both DB and MDB2, just note the prefix of the constant.

在DB和MDB2中都是一样的,只需要注意常量的前缀即可。

// set fetchmode
$db->setFetchMode(DB_FETCHMODE_ASSOC);
$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);

简单的选择(Simple SELECTs)

There are the methods to select one row, one column, one cell and a bunch of records. DB prefixes them with get, while MDB2 uses query.

有选择一行,一列,一个单元格和一堆记录的方法。 DB用get前缀它们,而MDB2使用query

// select several records and shove them into an array
$all = $db->getAll('SELECT * FROM people');
$all = $mdb2->queryAll('SELECT * FROM people');
// select one cell
$one = $db->getOne('SELECT name FROM people WHERE id = 1');
$one = $mdb2->queryOne('SELECT name FROM people WHERE id = 1');
// one row
$row = $db->getRow('SELECT * FROM people WHERE id = 1');
$row = $mdb2->queryRow('SELECT * FROM people WHERE id = 1');
// a column
$col = $db->getCol('SELECT name FROM people');
$col = $mdb2->queryCol('SELECT name FROM people');

报价值(Quoting values)

In DB, the suggested method to quote is quoteSmart(). In MDB2 it's quote() and it accepts a second parameter, which tells the type of the value to be quoted. If the second parameter is omitted, MDB2 will try to guess the type.

在DB中,建议的引用方法是quoteSmart() 。 在MDB2中,它是quote()并接受第二个参数,该参数告诉要引用的值的类型。 如果省略第二个参数,则MDB2将尝试猜测类型。

$one = $db->getOne(
'SELECT name FROM people WHERE id = '
. $db->quoteSmart(1)
);
$one = $mdb2->queryOne(
'SELECT name FROM people WHERE id = '
. $db->quote(1, 'integer')
);

序列表(Sequence tables)

If you use sequence tables, both libs will provide you with a nextId() method:

如果使用序列表,则两个库都将为您提供nextId()方法:

echo $db->nextId('people_db');
echo $mdb2->nextId('people_mdb2');

The only difference is that when DB creates a sequence table (with one field and one value), the name of the field is id, where MDB2 will use sequence. If you're translating an existing project to MDB2 like me, and the sequence tables are already created by DB, you have the option of renaming this field in the database for all sequence tables, or you can set an MDB2 option and you're good to go.

唯一的区别是,当DB创建一个序列表(具有一个字段和一个值)时,该字段的名称为id ,其中MDB2将使用sequence 。 如果要像我一样将现有项目转换为MDB2,并且序列表已经由DB创建,则可以选择为所有序列表在数据库中重命名此字段,或者可以设置MDB2选项,然后好去。

$mdb2->setOption('seqcol_name','id');

自动执行(Auto execute)

Say you have the data:

假设您有数据:

$data = array('id' => 5, 'name' => 'Cameron');

To auto-insert it using DB, you'd do:

要使用DB自动插入,您可以执行以下操作:

$db->autoExecute('people', $data, DB_AUTOQUERY_INSERT);

For MDB2, the auto execution is probably not considered an often-used feature, so it's not in the base instance. You need to load an additional module to have access to it:

对于MDB2,自动执行可能不被视为常用功能,因此它不在基本实例中。 您需要加载其他模块才能访问它:

$mdb2->loadModule('Extended');

Now you can

现在你可以

$mdb2->autoExecute('people', $data, MDB2_AUTOQUERY_INSERT);

The above will work in PHP5 only. In PHP4, due to the limited support of object overloading (Thanks again to Lukas for clarifying this!), you'd need to do:

以上仅适用于PHP5。 在PHP4中,由于对象重载的支持有限(再次感谢Lukas阐明了这一点!),您需要执行以下操作:

$mdb2->extended->autoExecute('people', $data, MDB2_AUTOQUERY_INSERT);

Note that the second way will also work in PHP5.

请注意,第二种方法也可以在PHP5中使用。

准备好的陈述 (Prepared statements)

In DB:

在数据库中:

$statement = $db->prepare('INSERT INTO people VALUES (?, ?)');
$data = array(6, 'Chris');
$db->execute($statement, $data);
$db->freePrepared($statement);

In MDB it's almost the same, only that the statement becomes an object and you call its (as opposed to MDB2 main object's) methods to execute and to release memory:

在MDB中,几乎是一样的,只是该语句成为一个对象,然后调用它的方法(与MDB2主对象相对)来执行和释放内存:

$statement = $mdb2->prepare('INSERT INTO people VALUES (?, ?)');
$data = array(7, 'Dave');
$statement->execute($data);
$statement->free();

执行多个(Execute multiple)

The same applies to executing a statement with with multiple "rows" of data from an array. executeMultiple() is in the Extended MDB2 module, so you need to load it:

这同样适用于执行带有数组中多个“行”数据的语句。 executeMultiple()在扩展MDB2模块中,因此您需要加载它:

DB:

D B:

$statement = $db->prepare('INSERT INTO people VALUES (?, ?)');
$data = array(
array(8, 'James'),
array(9, 'Cliff')
);
$db->executeMultiple($statement, $data);
$db->freePrepared($statement);

MDB2:

MDB2:

$statement = $mdb2->prepare('INSERT INTO people VALUES (?, ?)');
$data = array(
array(10, 'Kirk'),
array(11, 'Lars')
);
$mdb2->loadModule('Extended');
$mdb2->extended->executeMultiple($statement, $data);
$statement->free();

交易次数(Transactions)

In DB:

在数据库中:

$db->autoCommit();
$result = $db->query('DELETE people'); // will cause an error
if (PEAR::isError($result)) {
$db->rollback();     //echo 'rollback';
} else {
$db->commit();     //echo 'commit';
}

In MDB2 you have to check if transactions are supported in your RDBMS. Then during the transaction, you can always check "Am I in transaction?"

在MDB2中,您必须检查RDBMS中是否支持事务。 然后,在交易过程中,您始终可以选中“我正在交易吗?”

if ($mdb2->supports('transactions')) {
$mdb2->beginTransaction();
}
$result = $mdb2->query('DELETE people');
if (PEAR::isError($result)) {
if ($mdb2->in_transaction) {
$mdb2->rollback();         // echo 'rollback';
}
} else {
if ($mdb2->in_transaction) {
$mdb2->commit();         // echo 'commit';
}
}

示例脚本(Example script)

You can download a script that has the examples above and play with it. Here's also the sql file to recreate the database:

您可以下载包含以上示例的脚本并进行操作。 这也是重新创建数据库的sql文件:

  • db2mdb2.phps

    db2mdb2.phps

  • db2mdb2.sql

    db2mdb2.sql

Any questions or comments are welcome

mdb 转db_DB-2-MDB2相关推荐

  1. mdb数据库连接代码_重用与MDB2的现有数据库连接

    mdb数据库连接代码 This is a follow up to a question posted by Sam in my DB-2-MDB2 post. The question was if ...

  2. 将DBF,XLS,XML,MDB文件导入C#DataGrid的方法

    以下的源码里分别给出了将DBF,XLS,XML,MDB文件导入C#DataGrid的方法,供各位参考. //PutInDataSet.cs的源码 using System; using System. ...

  3. odbc里面没有Microsoft Access Driver(*.mdb)问题解决

    2019独角兽企业重金招聘Python工程师标准>>> 电脑的odbc里面没有Microsoft Access Driver(*.mdb),并且我已经装上了microsoft off ...

  4. server.mapPath(.mdb)

    server.mapPath(".mdb") 用http://www.google.com搜索server.mapPath(".mdb"),可以得到很多采用ac ...

  5. jboss5+EJB3+MDB Queue

    在使用jboss5进行MDB的试验时首先要在jboss5中配置jms 队列. 1)在jboss安装目录下:server\default\deploy\messaging 打开destinations- ...

  6. asp网络编程:用ASP打开远端MDB文件的方法

    如果你用ODBC connection (DSN or DSN-less)来访问远端的(UNC path)数据库, OLEDB会出现以下错误信息: Microsoft OLE DB Provider ...

  7. 微信小程序asp服务器架设,asp写的微信小程序支付demo-服务器端是asp+mdb的

    这个微信小程序支付demo代码是我用asp写的,微信小程序端加上服务器端用的asp和mdb数据库,下面是代码分享: 订单说明:{{paydata.title}} 支付金额:分 支付 --------- ...

  8. asp access的安全:不要认为简单的改后缀mdb为asp就能防下载

    asp access的安全:不要认为简单的改后缀mdb为asp就能防下载 昨天和animator试验了一下,把data.mdb文件改名为data.asp文件后放在wwwroot目录里.然后在IE中输入 ...

  9. mdb导入SqlServer

    弄了一份医案数据库,打开一看...命名全中文,好吧,导入SQLServer走起 SQL: SELECT * INTO newtable FROM OPENDATASOURCE ('Microsoft. ...

最新文章

  1. 京东姚霆:推理能力,正是多模态技术未来亟需突破的瓶颈!
  2. JAVA vs C++之速度—
  3. SAP云平台上的502 Bad Gateway错误
  4. 计算机桌面显示保护眼睛设置,电脑屏幕如何设置最保护眼睛
  5. jq之mouseup
  6. oracle诊断日志,oracle日常诊断语句
  7. windows活动目录与网络系列(1)
  8. 打造创新电磁诊疗技术平台,睿笛生物获比邻星创投、三捷资本数千万元投资...
  9. 函数名、闭包及迭代器
  10. (转)Fintech路上券商究竟做错了什么?漏做了什么?
  11. ajax 循环php数组,使用Jquery,AJAX,PHP和数组进行实时更新
  12. 动态规划精卫填海之路
  13. 国家多部委发布13份“十四五”规划,115项重大工程​
  14. 设计灵感|App登录注册页面设计方式
  15. 张一鸣宣布卸任字节跳动CEO,联合创始人梁汝波将接任
  16. Contrastive Adaptation Network for Unsupervised Domain Adaptation
  17. 企业数字化转型-从企业IT部门和CIO的数字化思想开始
  18. 超融合架构和服务器虚拟化是什么关系?主流超融合厂商服务器虚拟化产品对比分析
  19. 达人评测 索尼8K电视Z9J、4K电视X95J和83英寸A90J 怎么样
  20. 国内MEMS企业、研究所以及科研院校

热门文章

  1. pycharm怎么设置自己喜欢的背景
  2. 人民艺术家杨明受邀参加“中华情·走进青海”赴青海采风团活动
  3. 智慧工厂整体解决方案
  4. A-level 计算机科学学习记录:系统软件system software(全英)
  5. 我的人工智能之旅——线性回归
  6. 2014.9~2015.3 读书心得
  7. MySQL添加用户、为用户分配权限
  8. CRUSH: Controlled, Scalable, Decentralized Placement of Replicated Data译文
  9. css属性flex:1代表什么
  10. python--循环输出26个字母对应的ASCII的码值、模拟用户登录 、猜数游戏(二分法)、计算100-999之间的水仙花数