Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi Find all the records in the table and change the text (https://www.delphipraxis.net/172846-find-all-records-table-change-text.html)

danten 25. Jan 2013 10:10

Datenbank: Absolute Database • Version: 4 • Zugriff über: Absolute Database

Find all the records in the table and change the text
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hi everyone.
Please help with the code.
I need to retrieve all records entered in Edit1 and rewrite them according Edit2.

I use this code, but it overwrites all raises my records in the table.

Edit1.Text = Old string
Edit2.Text = New string


Delphi-Quellcode:
var
  Options: TLocateOptions;
begin
  Options := Options + [loPartialKey, loCaseInsensitive];
  Table1.Close;
  Table1.Open;
  Table1.Locate('Permission',Edit1.Text,Options);
  Table1.First;
  while not Table1.eof do
  begin
    loginForm.ABSTable1.Edit;
    loginForm.ABSTable1.FieldByName('Permission').AsString := Edit2.Text;
    loginForm.ABSTable1.Post;
    loginForm.ABSTable1.next;
    loginForm.ABSTable1.Locate('Permission',Edit1.Text,Options);
  end;

  ShowMessage('Permissions have been changed..');
Thank you all.

DeddyH 25. Jan 2013 10:15

AW: Find all the records in the table and change the text
 
I would prefer to use a query instead of a table and change the records by SQL.
Delphi-Quellcode:
Query.Close;
Query.SQL.Text := 'UPDATE Tablename SET Permission = :new WHERE Permission = :old';
Query.ParamByName('new').Value := Edit2.Text;
Query.ParamByName('old').Value := Edit1.Text;
Query.ExecSQL;

Angel4585 25. Jan 2013 10:20

AW: Find all the records in the table and change the text
 
Delphi-Quellcode:
var
  Options: TLocateOptions;
begin
  Options := Options + [loPartialKey, loCaseInsensitive];
  Table1.Close;
  Table1.Open;
  Table1.First;
  while Table1.Locate('Permission',Edit1.Text,Options) do
  begin
    Table1.Edit;
    Table1.FieldByName('Permission').AsString := Edit2.Text;
    Table1.Post;
  end;

  ShowMessage('Permissions have been changed..');
Didn't test it but should work.
Though the better version ist the one from DeddyH using SQL.

danten 25. Jan 2013 10:35

AW: Find all the records in the table and change the text
 
Yes, I'll try SQL.

Thank you;


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:33 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz