简体   繁体   English

如何将表中的值保存到另一个表delphi中

[英]How to save values from a table into another table delphi

I am using delphi rad studio and i am a beginner in this. 我正在使用delphi rad studio,我是初学者。 I am trying to insert values into a table from another table which contains just IDs. 我试图从另一个只包含ID的表中将值插入表中。 I have some comboboxes filled with ID's from a table and then i want to insert these values into another table. 我有一些组合框填充了表中的ID,然后我想将这些值插入另一个表中。 To be more specific i have a table that contains the specifications about cars and i want to save those into a table which contains information of rental this cars. 更具体地说,我有一张表格,其中包含有关汽车的规格,我想将这些表格保存到一张表格中,其中包含租车信息。 I have tried much but i didn't get the answer. 我已经尝试了很多,但我没有得到答案。 Here is the code for combobox : 这是组合框的代码:

    procedure TForm13.DBLookupComboBox1Click(Sender: TObject);
    begin
    adomarca.SQL.Text :=    ' SELECT tabel_Marca.Marca from Tabel_Masini ' +
     'join tabel_Marca on tabel_masini.id_Marca=tabel_marca.id_Marca ' ;

     SELF.ADOmarca.ExecSQL;
     SELF.ADOmarca.Open;
     ADOmarca.Prepared := TRUE;
     end;        

And this is the code for the save button: 这是保存按钮的代码:

     procedure TForm13.button2click(Sender: TObject);

      begin
      Comanda := 'Insert into Tabel_Inchirieri (id_Client, id_Marca, 
                id_Model, id_Transmisie, id_Caroserie, id_Culoare, 
                   id_Combustibil, Ziua_Inchirierii, Ziua_Restituirii, 
            Nr_zile_inchiriate) ' +
        // 'select * from tabel_masini ' +
         'Values (' + inttostr(DBLOOKUPCOMBOBOX8.keyvalue) + ',' 
         +inttostr(DBLOOKUPCOMBOBOX1.keyvalue) + ',' + 
          inttostr(DBLOOKUPCOMBOBOX2.keyvalue) + ',' + 
          inttostr(DBLOOKUPCOMBOBOX3.keyvalue) + ',' + 
          inttostr(DBLOOKUPCOMBOBOX4.keyvalue)+ ',' + 
           inttostr(DBLOOKUPCOMBOBOX5.keyvalue) + ',' + 
          inttostr(DBLOOKUPCOMBOBOX6.keyvalue)+ ',' + 
           quotedstr(formatdatetime('yyyy-mm-dd',DateTimePicker1.Date))+ 
          ','+ quotedstr(formatdatetime('yyyy-mm- 
           dd',DateTimePicker2.Date))+',' + 
          inttostr(daysbetween(DateTimePicker2.Date, 
        DateTimePicker1.Date))+')';
         Qcomanda.SQL.Add(Comanda);

         try
           Qcomanda.ExecSQL ;
         finally
            ShowMessage('Comanda a fost salvata cu succes ! ');

          end;


           end;

Thank you very much. 非常感谢你。

You don't say what RDMS you are using, but there are ways to do this entirely in SQL that should work with at least MS Sql Server and MS Access, which are probably the mostly commonly used RDMSs with ADO. 你没有说你正在使用什么RDMS,但有些方法可以完全在SQL中完成这项工作,至少应该使用MS Sql Server和MS Access,它们可能是ADO最常用的RDMS。

If you are intending your copying operation to populate a new table on the server you can use SQL like this 如果您打算在复制操作中填充服务器上的新表,则可以使用这样的SQL

   SELECT
     [FieldList]
   INTO
     MyNewTable
   FROM
     [tables and join condition]

You can easily google an example of this, see fi http://www.vb-helper.com/howto_ado_select_into.html 你可以轻松谷歌这样做的一个例子,请参阅fi http://www.vb-helper.com/howto_ado_select_into.html

If you want to copy rows into an existing table you can use the 如果要将行复制到现有表中,可以使用

   INSERT INTO
     [tablename]
   SELECT
     [fieldlist]
   FROM
     [tables and join condition]

see fi https://docs.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/insert-into-statement-microsoft-access-sql 见fi https://docs.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/insert-into-statement-microsoft-access-sql

You can execute these SQL statements either in a Delphi app or, if using MS SQL Server, using the SQL Server Management Studio. 您可以在Delphi应用程序中执行这些SQL语句,或者,如果使用MS SQL Server,则可以使用SQL Server Management Studio执行这些SQL语句。

Either method, unlike your proposed code, would copy the selected records all in one go, without any need for a record-by-record, column-by-column copying operation, and you should find either method extremely fast compared to client-side record-by-record copying. 与您提出的代码不同,这两种方法都可以一次性复制所选记录,而无需逐个记录,逐列复制操作,并且您应该找到与客户端相比极快的方法按记录复制。 However, you need to be careful if a very large number of records is involved: doing them all in one go means that they are potentially wrapped up in a huge server-side transaction which could exhaust the server's resources, so in that situation it would probably be better to split the copying up into a series of smaller batches, if that's practicable. 但是,你必须要小心,如果非常大量的记录涉及:在一个去做所有这些意味着它们可能在一个巨大的服务器端的交易可能消耗服务器的资源包裹起来,所以在这种情况下它会如果可行的话,最好将复制分成一系列较小的批次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM