指南:从MySQL转向ADODB(2)

王朝mysql·作者佚名  2006-01-08
宽屏版  字体: |||超大  

高级材料

Insert 和 Update

假设现在你要把下面的数据插入到数据库中.

ID = 3

TheDate=mktime(0,0,0,8,31,2001) /* 31st August 2001 */

Note= sugar why don't we call it off

当你使用另外一个数据库的时候,你的插入操作可能不会成功。

第一个问题是每个数据库都有不同的默认日期格式。MySQL默认格式是YYYY-MM-DD ,

然而其它数据库有不同的默认格式。ADODB有一个DBDate()的函数,它能将日期转

换成正确的格式。

接下来的一个问题是Note字段值里的don't 应该作为引文。在MySQL中,使用

don\'t 来解决这个问题,但在一些其它的数据库中(Sybase, Access, Microsoft

SQL Server)使用don''t 。qstr()解决了这个问题。

那么我们怎么使用这个函数呢? 像下面这样:

$sql = "INSERT INTO table (id, thedate,note) values ("

. $ID . ','

. $db->DBDate($TheDate) .','

. $db->qstr($Note).")";

$db->Execute($sql);

ADODB 也支持 $connection->Affected_Rows() (返回上次update或delete操作影响的

行数) 和 $recordset->Insert_ID() (返回insert声明生成的上一个自增编号)。但要

说明的是不是所有的数据库都支持这两个函数。

MetaTypes

你可以找到更多关于你调用recordset的方法FetchField($fieldoffset)所选择的每个

字段(我同时使用字段和列这两个词)的信息。它将返回一个有三个属性(名称,类

型和最大长度)的对象。

例如:

$recordset = $conn->Execute("select adate from table");

$f0 = $recordset->FetchField(0);

那么 $f0->name 的值将被设为 'adata', $f0->type 的值将被设为 'date'. 如果

max_length 未知,它被设为-1。

处理不同类型的数据库的一个问题是每个数据库常常用不同的名字来调用相同的类型。

例如timestamp 类型在某一个数据库中叫做datetime 类型,而在另一个是叫做time

类型。 因此 ADODB 有个专门的 MetaType($type, $max_length) 函数对下面的类型

进行标准化:

C: character 和 varchar 类型

X: text 或者 long character (例如.多于255 字节宽度).

B: blob 或者 binary 图像

D: date

T: timestamp

L: logical (boolean)

I: integer

N: numeric (float, double, money)

在上面的例子中,

$recordset = $conn->Execute("select adate from table");

$f0 = $recordset->FetchField(0);

$type = $recordset->MetaType($f0->type, $f0->max_length);

print $type; /* 应该显示 'D' */

Select Limit 和 Top 支持

ADODB 有一个叫$connection->SelectLimit($sql,$nrows,$offset)的函数,它允许你

获得一个记录集(recordset)的子集。它会在Microsoft产品上使用本地的SELECT TOP,

在PostgreSQL和MySQL上使用SELECT ... LIMIT,并在不支持这项功能的数据库上模拟

这个功能。

缓存支持

ADODB 允许你在你的文件系统上缓存记录集结果,只需用$connection->CacheExecute($secs2cache,$sql)

和$connection->CacheSelectLimit($secs2cache,$sql,$nrows,$offset)在指定的超时

时间段后重新向数据库服务器发出查询。

PHP4 Session 处理接口支持

ADODB 也支持PHP4 session 处理接口. 你可以使用ADODB在一个数据库中存储你的

session变量。要获得更多下信息,访问[/url][url=http://php.weblogs.com/adodb-sessions]http://php.weblogs.com/adodb-sessions

鼓励作为商业用途

如果你正计划编写你要转售的商业PHP软件产品,你应该考虑ADODB。ADODB使用GPL发

布。这意味这你也可以在商业软件中合法的使用它,同时保持你的代码所有权。十分

鼓励ADODB作为商业用途!因为上述的原因我们内部也正在使用它。

结束语

作为你能耐心的看完这篇文章的答谢,下面是let's call the whole thing off 的

完整歌词:

Refrain

You say eether and I say eyether,

You say neether and I say nyther;

Eether, eyether, neether, nyther -

Let's call the whole thing off !

You like potato and I like po-tah-to,

You like tomato and I like to-mah-to;

Potato, po-tah-to, tomato, to-mah-to -

Let's call the whole thing off !

But oh, if we call the whole thing off, then we must part.

And oh, if we ever part, then that might break my heart.

So, if you like pajamas and I like pa-jah-mas,

I'll wear pajamas and give up pa-jah-mas.

For we know we

Need each other, so we

Better call the calling off off.

Let's call the whole thing off !

Second Refrain

You say laughter and I say lawfter,

You say after and I say awfter;

Laughter, lawfter, after, awfter -

Let's call the whole thing off !

You like vanilla and I like vanella,

You, sa's'parilla and I sa's'parella;

Vanilla, vanella, choc'late, strawb'ry -

Let's call the whole thing off !

But oh, if we call the whole thing off, then we must part.

And oh, if we ever part, then that might break my heart.

So, if you go for oysters and I go for ersters,

I'll order oysters and cancel the ersters.

For we know we

Need each other, so we

Better call the calling off off.

Let's call the whole thing off !

George 和 Ira Gershwin作词作曲, 由Fred Astaire 和 Ginger Rogers在电影

"Shall We Dance?"介绍给大家

(c)2001-2002 John Lim.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有