检查数据库表中的唯一值时出错? [英] Error checking for unique value in database table?

查看:121
本文介绍了检查数据库表中的唯一值时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据插入到包含5个字段的用户表中,听起来很简单

正常,但其中2个字段被指定为UNIQUE。如果有人

确实输入了一个已经存在的值,我如何捕获这个特定的

错误?


会不会更多首先实际运行一个SELECT查询,如果那个
返回了一个结果,那么我用它来进行错误检查而不用运行插入

直到select返回任何内容?


干杯


菲尔

I am inserting data into user table which contains 5 fields, sounds simple
enough normally but 2 of the fields are designated as UNIQUE. If someone
does enter a value which already exists, how do I capture this specific
error?

Would it make more sense to actually run a SELECT query first and if that
returned a result, then I use that for error checking and don''t run insert
until select returns nothing?

Cheers

Phil

推荐答案

Phil Latio写道:
Phil Latio wrote:

我将数据插入到包含5个字段的用户表中,听起来很简单

通常足够但是2个字段被指定为UNIQUE。如果某人

确实输入了一个已经存在的值,我该如何捕获这个特定的

错误?
I am inserting data into user table which contains 5 fields, sounds simple
enough normally but 2 of the fields are designated as UNIQUE. If someone
does enter a value which already exists, how do I capture this specific
error?



您好,


您可以使用连接上的错误消息来捕获错误。

如何命名函数取决于您使用的数据库,或者

数据库抽象层。

Hi,

You capture the error by using the errormessage on your connection.
How the function is named depends on the database you are using, or the
database abstraction layer.


>

首先实际运行SELECT查询更有意义吗?如果

返回结果,那么我用它进行错误检查而不运行插入

直到select返回什么?
>
Would it make more sense to actually run a SELECT query first and if that
returned a result, then I use that for error checking and don''t run insert
until select returns nothing?



是的。

我想说这是用风格做的正确方法。


你当然可以尝试解析你的错误消息并寻找子串

喜欢''U​​NIQUE CONSTRAINT VIOLATION''(只是一个例子),但那条消息

可能非常如果你切换数据库,或者升级你现在使用的那个
就好了。


所以我会选择你自己描述的一般解决方案:先检查

违反UNIQUE约束,然后插入。


问候,

Erwin Moller

Yes.
I would say that is the right way of doing it with style.

You could of course try to parse your errormessage and look for substrings
like ''UNIQUE CONSTRAINT VIOLATION'' (just an example), but that message
could very well be different if you switch databases, or upgrade the one
you use now.

So I would go with the general solution you described yourself: check first
for violation of UNIQUE constraint, then insert.

Regards,
Erwin Moller


>

干杯


Phil
>
Cheers

Phil


Phil Latio写道:
Phil Latio wrote:

我将数据插入到包含5个字段的用户表中,听起来很简单

通常足够但是2个字段是指定为UNIQUE。如果有人

确实输入了一个已经存在的值,我如何捕获这个特定的

错误?


会不会更多首先实际运行一个SELECT查询,如果那个
返回了一个结果,那么我用它来进行错误检查而不用运行插入

直到select返回任何内容?


干杯


菲尔


I am inserting data into user table which contains 5 fields, sounds simple
enough normally but 2 of the fields are designated as UNIQUE. If someone
does enter a value which already exists, how do I capture this specific
error?

Would it make more sense to actually run a SELECT query first and if that
returned a result, then I use that for error checking and don''t run insert
until select returns nothing?

Cheers

Phil




Phil,


查看数据库返回的错误代码。


大多数数据库都会返回错误代码和错误消息。错误

信息非常适合人类;错误代码更适合计算机,并且

应该是唯一的(IIRC,这个是1062 for MySQL)。


不要先选择。这是不必要的开销,并且不保证

,除非你先锁定表(更多的开销)。例如,你

可能会搜索john。并没有找到它 - 但在你添加john之前,

另一个进程添加它。现在当你尝试的时候,虽然你先检查了,但你得到的是重复的,甚至是




-

==== ==============

删除x来自我的电子邮件地址

Jerry Stuckle

JDS计算机培训公司
js ******* @ attglobal.net

==================

Phil,

Look at the error code returned by the database.

Most databases return an error code and an error message. The error
message is great for humans; the error code better for computers, and
should be unique (IIRC, this one is 1062 for MySQL).

Don''t do a select first. It''s unnecessary overhead and not guaranteed
unless you lock the tables first (more overhead). For instance, you
might search for "john" and not find it - but before you can add "john",
another process adds it. Now when you try, you get a duplicate, even
though you checked first.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


Erwin Moller写道:
Erwin Moller wrote:

您当然可以尝试解析错误消息并查找子字符串

比如''UNIQUE CONSTRAINT VIOLATION''(只是一个例子),但是如果您切换数据库,或者升级现在使用的那个
,那么消息

可能会有所不同。
You could of course try to parse your errormessage and look for substrings
like ''UNIQUE CONSTRAINT VIOLATION'' (just an example), but that message
could very well be different if you switch databases, or upgrade the one
you use now.



一个很好的折衷方案是解析错误信息,但是如果它无法识别
,则显示错误信息直接给用户。


-

Toby A Inkster BSc(荣誉)ARCS

[极客HTML / SQL / Perl / PHP / Python / Apache / Linux]

[操作系统:Linux 2.6.12-12mdksmp,最多88天,18:28。]


伟大的Wi-Fi争议
http: //tobyinkster.co.uk/blog/2007/05/22/wifi-scare/


这篇关于检查数据库表中的唯一值时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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