delphi Ado(mdb)更新记录

发布于 2024-08-29 05:46:31 字数 998 浏览 0 评论 0原文

我正在尝试从一个主表和另外 2 个子表复制数据。当我在主表中选择一条记录时,我会将该表中的所有字段复制到另一条记录中。 (Table1从ADOQuery复制所选记录)

procedure TForm1.copyButton7Click(Sender: TObject);
SQL.Clear;
SQL.Add('SELECT * from ADoquery');
SQL.Add('Where numeracao LIKE ''%'+NInterv.text);// locate record selected in Table1 NInterv.text)
Open;

 // iniciate copy of record´s

begin
   while not tableADoquery.Eof do

      begin
         Table1.Last;
         Table1.Append;// how to append if necessary!!!!!!!!!!
         Table1.Edit;
         Table1.FieldByName('C').Value := ADoquery.FieldByName('C').Value;
         Table1.FieldByName('client').Value := ADoquery.FieldByName('client').Value;
         Table1.FieldByName('Cnpj_cpf').Value := ADoquery.FieldByName('Cnpj_cpf').Value;
         table1.Post; 
         table2.next;///
       end;
end;

//我如何同时从TableChield_1和TableChield_2字段更新TableChield,TableChield1?

对子表执行同样的操作 TableChield <= TableChield_1
TableChield1 <= TableChield_2

谢谢

I´m trying to copy data from one master table and 2 more child tables. When I select one record in the master table I copy all the fields from that table for the other. (Table1 copy from ADOQuery the selected record)

procedure TForm1.copyButton7Click(Sender: TObject);
SQL.Clear;
SQL.Add('SELECT * from ADoquery');
SQL.Add('Where numeracao LIKE ''%'+NInterv.text);// locate record selected in Table1 NInterv.text)
Open;

 // iniciate copy of record´s

begin
   while not tableADoquery.Eof do

      begin
         Table1.Last;
         Table1.Append;// how to append if necessary!!!!!!!!!!
         Table1.Edit;
         Table1.FieldByName('C').Value := ADoquery.FieldByName('C').Value;
         Table1.FieldByName('client').Value := ADoquery.FieldByName('client').Value;
         Table1.FieldByName('Cnpj_cpf').Value := ADoquery.FieldByName('Cnpj_cpf').Value;
         table1.Post; 
         table2.next;///
       end;
end;

//How can i update the TableChield,TableChield1 from TableChield_1 and TableChield_2 fields at the same time?

do the same for the child tables
TableChield <= TableChield_1
TableChield1 <= TableChield_2

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2024-09-05 05:46:31

这些字段将同时更新。实际更新是在您调用 post 时执行的(或者甚至不执行,这取决于批量更新是否打开)。

但请重新考虑一下你的逻辑。使用 SQL 语句 (INSERT) 将数据插入到另一个表中会更有效,

SQL.Clear;
SQL.Add('INSERT INOT TABLE_1(C, client, Cnpj_cpf)');
SQL.Add('VALUES(:C, :client, :Cnpj_cpf)');

然后只需在循环中填充值即可。

SQL.Parameters.ParamByName('C').Value := ADoquery.FieldByName('C').Value;
SQL.Parameters.ParamByName('client').Value := ADoquery.FieldByName('client').Value;
SQL.Parameters.ParamByName('Cnpj_cpf').Value := ADoquery.FieldByName('Cnpj_cpf').Value; 
SQL.ExecSQL;

如果数据已经位于目标表中,您还可以执行更新 - 插入模式。

像这样:

if SQL.ExecSQL = 0 then
begin
  // no records were update, do an insert
end;

并且表明您正在将数据从表 1 复制到表 2 可能是设计缺陷的迹象。但在不了解更多情况的情况下,我不能肯定地说。无论如何,数据重复从来都不是好事。

The fields will all be updated at the same time. The actual update is performed when you call post (or not even then, it depends if the Batch Updates are on or off).

But please reconsider your logic. It would be far more efficient to use SQL statements (INSERT) in order to insert the data to the other table

SQL.Clear;
SQL.Add('INSERT INOT TABLE_1(C, client, Cnpj_cpf)');
SQL.Add('VALUES(:C, :client, :Cnpj_cpf)');

Then just fill the values in a loop.

SQL.Parameters.ParamByName('C').Value := ADoquery.FieldByName('C').Value;
SQL.Parameters.ParamByName('client').Value := ADoquery.FieldByName('client').Value;
SQL.Parameters.ParamByName('Cnpj_cpf').Value := ADoquery.FieldByName('Cnpj_cpf').Value; 
SQL.ExecSQL;

You can also do the Updade - Insert pattern if the data can alredy be in the target table.

Like This:

if SQL.ExecSQL = 0 then
begin
  // no records were update, do an insert
end;

And also the indication that you are copying data from table 1 to table 2 could be a sign of design flaw. But I can't say that for sure without knowing more. Anyway data duplication is never good.

征棹 2024-09-05 05:46:31

我相信提问者正在考虑数据完整性,这意味着,确保只有所有表都会更新或没有......

我知道安全地实现这一点的方法是使用 SQL 命令执行所有这些更新(或插入,aso)在过渡内。

I believe the asker was thinking about the data integrity, that means, ensure that only all the tables will updated or none...

The way I know to achieve this with security is executing all this updates (or inserts, a.s.o.) using SQL commands inside a transition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文