Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 12-04-2006, 02:20 AM
Bharathi Bharathi is offline
Senior Member
 
Join Date: Oct 2006
Posts: 120
Default table update

i try to update the age for fields ID=100;

i try this code

table1.open;
no:= table1.RecordCount;
table1.first;
while i:=0 to no do
begin
if table1.locate('ID',currID,[]) then
begin
table1.edit;
table1AGE.text:=Age;
table1.post;
end;
table1.next;
end;
table1.refresh;


but it updates only the first record for Particular ID . it not upadtes the second record.

i have my table structure like this(its example data,ny table has different data )

ID Age
100 12
100 12
101 33
101 33
100 12
100 12

how do update the Age field for a particular ID.
Reply With Quote
  #2  
Old 12-04-2006, 02:27 AM
davidj davidj is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,900
Default RE: table update

You are locating currID

Where does that come from as in your code it doesnt
get changed?
Reply With Quote
  #3  
Old 12-04-2006, 02:34 AM
digitiger digitiger is offline
Senior Member
 
Join Date: Jan 2001
Posts: 716
Default RE: table update

First of all what database do you use?

Secondly you dont have to get the recordcount of the table and then run the loop because recordcount is not really supported by most of the database and sometimes it does not gives you the correct number of records

instead you can run the loop as follows:

table1.first;
while not table1.eof do
begin

blah blah blah....
table1.next
end;

Secondly, if you need to update all the records with particular value in given field then simplest and fastest way to do that is using an update query.

you can just add a query component and write its sql as follows:

'Update ' + quotedStr(Table1.tablename) + ' set Age = ' + quotedStr(Age) + ' where ID = ' + QuotedStr(CurrID)


Above assumes that the age as well as ID fields are string types else you will have to ommit the quotedStr() part .

and when you want to execute the above statement just say

query1.execute (AND REMEMBER NOT TO CALL QUERY1.OPEN)

In nutshell the final SQL statement should look like the following (supposing table name is AgeData you want to set the age to 'THIRTY' for all thE IDs with values as "ID01abc'

Update AgeData set Age = "Thirty" where ID = "ID01abc"



Hope it helps and if it does then do accept the answere
Reply With Quote
  #4  
Old 12-04-2006, 02:36 AM
davidj davidj is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,900
Default RE: table update

You could do this with SQL much simpler

[SQL]Query1.SQL.Clear;
Query1.SQL.Add('UPDATE YourTable SET AGE = :Age WHERE ID = :CurrID');
Query1.ParamByName('AGE').AsInteger := Age;
Query1.ParamByName('CurrID').AsInteger := CurrID;
Query1.ExecSQL;[/SQL]
Reply With Quote
  #5  
Old 12-04-2006, 02:37 AM
davidj davidj is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,900
Default RE: table update

Sorry mate, was still typing when you wrote that.
Reply With Quote
  #6  
Old 12-04-2006, 08:08 PM
Bharathi Bharathi is offline
Senior Member
 
Join Date: Oct 2006
Posts: 120
Default RE: table update

i try this

Query1.sql.clear;

q:='UPDATE table1 SET AGE ='''+Age+'''WHERE ID= '''+currID+'''';
Query1.sql.add(q);
Query1.execSQL;
Query1.active:True;

it works
Reply With Quote
  #7  
Old 12-04-2006, 10:34 PM
davidj davidj is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,900
Default RE: table update

You dont need to Query1.Active := True;

This means you are executing the SQL twice.

Just use Query1.ExecSQL;
As no result set is returned.

Your changes suggest that Age and CurrID are Strings?

Anyway glad you got it working.
Dave
Reply With Quote
  #8  
Old 12-04-2006, 10:47 PM
Bharathi Bharathi is offline
Senior Member
 
Join Date: Oct 2006
Posts: 120
Default RE: table update

thank you,
Dave
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 02:56 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.