?
Solved

SEE IF IS THERE SAME FIELD IN SQL DATABASE

Posted on 2006-03-29
8
Medium Priority
?
388 Views
Last Modified: 2010-04-05
I´M TRYING TO INSERT ROW IN MY DATABASE, FIELDS LIKE NAME AND USER CAN NOT EXIST SAME VALUES FOR THIS FIELD. IF EXIST, MY APPLICATION HAVE TO SHOW MESSAGE TO USER LIKE: "YOU HAVE TO CHANGE YOU USER NAME".

HOW CAN I DO THAT.
0
Comment
Question by:rafaelrgl
  • 4
  • 3
8 Comments
 

Expert Comment

by:danielamoura
ID: 16328186
The best way to always guarantee you won't have duplications is by adding an unique constrain to your database on the respective fields. If you are using for example MS SQL Server, this would be the syntax:

alter TABLE test ADD CONSTRAINT uqName UNIQUE (Name)
alter TABLE test ADD CONSTRAINT uqUser UNIQUE (User)

Than, just trap the exception


0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 16328428
WHERE CAN I DO THAT
0
 

Expert Comment

by:danielamoura
ID: 16328467
add the constrain to the database or trap the exception?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:rafaelrgl
ID: 16328739
MY FIELDS ON TB_USER IS

ID
USER
NAME

HOW IS GOING TO BE MY CONTRAINT EXPRESSION FOR DO THAT.
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 16328750
YOUR NAME CAME FROM BRASIL.
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 16328835
THANKS

I DID, BUT RIGHT NOW, I DON´T KNOW HOW CAN I TRAP THE EXCEPTION.
CAN YOU SHOW ME.

0
 

Expert Comment

by:danielamoura
ID: 16329262
Hi Rafael,
  I am brazilian. And I'm assuming you are too, but I will not switch to english, in case anyone else has the same problem as you

  try
      InsertInTable;
  except on E:EDataBaseError do
     
  end;

where InsertInTable is your method to insert in the table.

Daniela
0
 
LVL 1

Accepted Solution

by:
markaurelius earned 2000 total points
ID: 16329311
I use a query that does something like this

if GetIntValue('SELECT Count(*) FROM SomeTable WHERE USername = ' + UserName) > 0 then begin
  MessageDlg('Name already in use', mtWarning, [mbOK], 0);
  exit;
end;
...

where GetIntValue creates a  queryobject and runs the query to get the first field:


function TdConnection.GetIntValue(const sql: string): integer;
var query: TDataSet;
begin
  query := CreateQuery(sql);
  try
    result := query.Fields[0].AsInteger;
  finally
    query.Free;
  end;
end;// GetIntValue ---------------------

CreateQuery in for me uses TADOObject, but it would depend on what suite of database objects you are using. I've done the same sort of thing with IBExpress.

I prefer doing this to letting the program raise an exception, because that way I leave break on exception on in case a real problem arises.

HTH,
Mark
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question