Topic: Read data from external DB

I create a SQL script to transfer data from another database to the current one.
How can I retrieve an ID if they do not have serial numbers.

Re: Read data from external DB

Hi,

If there is no serial number then I don't know. )))
How can we help you if we don't know what a serial number is and what tables you have? And what are we talking about?


It is possible more in detail?

3 (edited by pavlenko.vladimir.v 2022-06-28 18:22:23)

Re: Read data from external DB

iwkom wrote:

I create a SQL script to transfer data from another database to the current one.
How can I retrieve an ID if they do not have serial numbers.

подключитесь к другой базе данных SQLite
connect to another SQLite database

SQLExecute('ATTACH DATABASE ''d:\testDB.db'' as ''TEST''');

   
использовать в программе
use in the program

SQLExecute('SELECT TEST.person.lastname FROM TEST.person');

4 (edited by iwkom 2022-06-28 19:41:06)

Re: Read data from external DB

In this way, only one record is extracted.
I want to read the whole table and save it in another database.
The ID numbers are not consecutive because there are deleted records.

Post's attachments

Attachment icon example.rar 361.2 kb, 217 downloads since 2022-06-28 

Re: Read data from external DB

iwkom wrote:

In this way, only one record is extracted.
I want to read the whole table and save it in another database.
The ID numbers are not consecutive because there are deleted records.

1. Подключаетесь к БД
2. Получаете общее число записей
3. С помощью (FOR) перебираете все записи  ( получить запись из таблицы1 -- создать новую запись в таблице2 )
     
1. Connect to the database
2. Get the total number of records
3. Use (FOR) to iterate through all the records ( get a record from table1 -- create a new record in table2 )

Re: Read data from external DB

iwkom wrote:

In this way, only one record is extracted.
I want to read the whole table and save it in another database.
The ID numbers are not consecutive because there are deleted records.


procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
  tmpDataSet: TDataSet;
begin
   SQLExecute('ATTACH DATABASE ''test.db'' as ''TEST''');
   SQLQuery('SELECT * FROM TEST.klienti ',tmpDataSet);
   try
     while not tmpDataSet.EOF do
     begin
       SQLExecute('INSERT INTO kl (nameklient) VALUES ('''+escape_special_characters(tmpDataSet.FieldByName('klient').asString) +''')');
       tmpDataSet.Next;
     end;
   finally
     tmpDataSet.Free;
   end;
end;
Визуальное программирование: блог и телеграм-канал.