Solved

insert and update

Posted on 2002-03-03
3
260 Views
Last Modified: 2010-05-18
i have a text file which i want to input the data in it to a database, i means the new data be inserted and if there exists old ones, it be updated without the "key violation " error.

how to do it?

thansk

mzlxkats
0
Comment
Question by:mzlxkats
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6838458
If your database is uniquely indexed, then you can do

if newdatakey=dbkey then
  update the database
else
  insert into database

If you're using a Database which has stored procedures, you can do this in one line of code in Delphi :)

Hope this helps,

Stu
0
 

Author Comment

by:mzlxkats
ID: 6840431
hello stu

do you have code more in detail?
0
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 50 total points
ID: 6845331
if you have a table that looks like this:

MYKEY         FIELD1   FIELD2   FIELD3
1             DAH      0        TRUE
2             DOO      100      TRUE
3             DEE      -5       FALSE

and you want to add a new record if one doesn't already exist, BUT update a record where it does already exist, then try this:

procedure DoIt(var NewKey: Integer; Field1: String; Field2: Integer; Field3: Boolean);
var
  Exists: Boolean;

begin
  Exists := false;
  with TQuery.Create(nil) do
    try
      DatabaseName := 'WHATEVER';
      SQL.Add('select count(DBKEY) from TABLENAME');
      SQL.Add('where DBKEY=:DBKEY');
      Prepare;
      ParamByName('DBKEY').asInteger := NewKey;
      Open;  
      Exists := RecordCount > 0;
      Close;
      SQL.Clear;  
      if Exists then
        begin
          SQL.Add('update TABLENAME');
          SQL.Add('set FIELD1=:FIELD1,');
          SQL.Add('FIELD2=:FIELD2,');
          SQL.Add('FIELD3=:FIELD3');
          SQL.Add('where DBKEY=:DBKEY');
        end
      else
        begin
          SQL.Add('insert into TABLENAME');
          SQL.Add('DBKEY, FIELD1, FIELD2, FIELD3)');
          SQL.Add('values (:DBKEY, :FIELD1, :FIELD2, :FIELD2');
        end;
      Prepare;
      ParamByName('DBKEY').asInteger := NewKey;
      ParamByName('FIELD1').asString := Field1;
      ParamByName('FIELD2').asInteger := Field2;
      ParamByName('FIELD3').asBoolean := Field3;
      try
        ExecSQL;
      except
        on E: Exception do
          MessageDlg(E.Message, mtError, [mbOK], 0);
      end;
    finally
      Free; {TQuery}
    end;
end;

This is coded from memory, so it may need a bit of correcting to run (but not much!).

The stored procedure issue is something different again.  It depends on the database you're using.  I'm only familiar with MS-SQL Server, so if you're using that, I can definately post the SQL for that and the Delphi code.

Hope that helps,

Stu
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

Suggested Solutions

Title # Comments Views Activity
oracle global variables 4 80
Delphi Ado Query To Converts Joined Columns Into Rows Based on Primary Key 4 94
Firemonkey Queue list 1 51
Posting a string with PostMessage 23 49
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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