数据库抽象层 [英] Database abstraction layers

查看:93
本文介绍了数据库抽象层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


那里有很多数据库抽象层,但快速浏览一下

它们还没有出现任何似乎符合我要求的东西。在我开始之前我会写一个我认为我会在这里询问是否有人知道这样一个

野兽......


我想要一些代码,其中我提供了一个数据数组,以及相应的

主键,让代码解决是否INSERT或UPDATE它,我

也希望能够从QBF或QBE中呈现数据,然后能够在结果集中逐步执行
。但是我不想配置

DBMS结构 - 毕竟大部分已经在DBMS中了(好吧所以不是MySQL中的

关系) D b)。如果我能直接将SQL

扔到它上面,那真的很酷*。


有人有什么建议吗?

TIA,


C.

解决方案

----- BEGIN PGP SIGNED消息-----

Hash:SHA1

Colin McKinnon写道:

我想要一些代码我提供了一个数组数组,以及相应的主键,让代码解决是否要INSERT或UPDATE它,[...]
[...]任何人有什么建议吗?



使用MySQL和REPLACE语句。


- -

- ------- ---------------------------
$ b $bIvánSánchezOrtega-i-punto-sanchez - arroba-mirame- punto-net


Un ordenador no es un televisor ni un microondas,es una herramienta

compleja。

---- -BEGIN PGP SIGNATURE -----

版本:GnuPG v1.4.3(GNU / Linux)


iD8DBQFEY8CT3jcQ2mg3Pc8RAl57 AJ901ARs69nLk3mptIDYKl fLDCeBogCfft10

HOi1URDxXXfp8rNg1RoHIGI =

= 93vZ

----- END PGP SIGNATURE -----

Iv ?? n S ?? nchez Ortega写道:

Colin McKinnon写道:

我想要一些我提出的代码一个数组数组,以及相应的主键,让代码解决是否INSERT或UPDATE它,[...]


[...]

有人有什么建议吗?



使用MySQL和REPLACE语句。




非常抽象。


OP的一种技术可能是准备INSERT和UPDATE

语句。运行INSERT语句;如果它失败那么那个主键可能已经有了一行,所以运行UPDATE语句。或者你

可以先运行UPDATE,检查更新的行数;如果为0,则运行

INSERT。不是特别干净,但应该非常便携。


-

Toby A Inkster BSc(荣誉)ARCS

与我联络〜 http://tobyinkster.co.uk/contact


Colin McKinnon写道:


那里有很多数据库抽象层,但快速浏览一下
他们没有出现任何符合我要求的东西。


这可能意味着你甚至不应该尝试用抽象层来解决你的问题。更简单的解决方案可能是

可用...

我想要一些代码,我提供一个数据数组,以及相应的主键,让代码工作是否要插入或更新它




你不需要PHP代码;这可以在数据库上处理

级别:


INSERT INTO the_table

SET field1 = value1,field2 = value2,field3 = value3,...

ON DUPLICATE KEY UPDATE field1 = value1,field2 = value2,field3 = value3,

....


从MySQL 4.1.0开始提供此语法。


您也可以使用REPLACE查询,但请记住它的工作原理是

删除旧记录并插入一个新记录,如果表中存在字段

,但查询中没有,则它们将丢失。


干杯,

NC


Hi all,

There''s lots of DB abstraction layers out there, but a quick look around
them hasn''t turned up anything which seems to met my requirements. Before I
go off and write one I thought I''d ask here if anyone knows of such a
beast...

I want some code where I present an array of data, and the corresponding
primary key and let the code work out whether to INSERT or UPDATE it, I
also want to be able to present the data from a QBF or QBE then be able to
step through the result set. However I don''t want to have to configure the
DBMS structure - after all most of it is already in the DBMS (OK so not the
relationships in a MySQL db). It''d be really cool if I could throw SQL
directly at it *too*.

Anybody any suggestions?

TIA,

C.

解决方案

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Colin McKinnon wrote:

I want some code where I present an array of data, and the corresponding
primary key and let the code work out whether to INSERT or UPDATE it,[...] [...] Anybody any suggestions?



Use MySQL and REPLACE statements.

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFEY8CT3jcQ2mg3Pc8RAl57AJ901ARs69nLk3mptIDYKl fLDCeBogCfft10
HOi1URDxXXfp8rNg1RoHIGI=
=93vZ
-----END PGP SIGNATURE-----


Iv??n S??nchez Ortega wrote:

Colin McKinnon wrote:

I want some code where I present an array of data, and the corresponding
primary key and let the code work out whether to INSERT or UPDATE it,[...]


[...]

Anybody any suggestions?



Use MySQL and REPLACE statements.



Very abstract.

One technique for the OP might be to prepare both an INSERT and an UPDATE
statement. Run the INSERT statement; if it fails then there''s probably
already a row with that primary key, so run the UPDATE statement. Or you
could run UPDATE first, check the number of rows updated; if 0, run the
INSERT. Not particularly clean, but should be quite portable.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact


Colin McKinnon wrote:


There''s lots of DB abstraction layers out there, but a quick look around
them hasn''t turned up anything which seems to met my requirements.
Which probably means that you shouldn''t even attempt to solve your
problem with an abstraction layer; easier solutions might be
available...
I want some code where I present an array of data, and the corresponding
primary key and let the code work out whether to INSERT or UPDATE it



You don''t need PHP code for that; this can be handled on the database
level:

INSERT INTO the_table
SET field1=value1, field2=value2, field3=value3, ...
ON DUPLICATE KEY UPDATE field1=value1, field2=value2, field3=value3,
....

This syntax is available since MySQL 4.1.0.

You could also use a REPLACE query, but keep in mind that it works by
deleting the old record and inserting a new one, so if there are fields
that exist in the table, but not in the query, they will be lost.

Cheers,
NC


这篇关于数据库抽象层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆