Copying a SQLITE table from one database to another database

Discussion of open issues, suggestions and bugs regarding LiteDAC (SQLite Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
jodyatfsec
Posts: 8
Joined: Wed 06 Nov 2013 13:41

Copying a SQLITE table from one database to another database

Post by jodyatfsec » Mon 04 May 2015 14:56

I want to copy a SQLITE table from one database to another database using a TLiteQuery. What is the syntax for the SQL? Thanks.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Copying a SQLITE table from one database to another database

Post by AlexP » Tue 05 May 2015 10:38

Hello,

Below is a sample of copying data from one database to another:

Code: Select all

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, LiteAccess;

var
  LiteConnection: TLiteConnection;
  LiteQuery: TLiteQuery;
begin
  LiteConnection := TLiteConnection.Create(nil);
  try
    LiteConnection.Options.ForceCreateDatabase := True;
    LiteConnection.Database := 'd:\src.db3';
    LiteConnection.Connect;
    LiteConnection.ExecSQL('create table t_test (f_id integer)');
    LiteConnection.ExecSQL('insert into t_test values(1),(2),(3),(4),(5)');
    LiteConnection.Disconnect;
    LiteConnection.Database := 'd:\dst.db3';
    LiteConnection.Connect;
    LiteConnection.ExecSQL('create table t_test (f_id integer)');
    LiteConnection.ExecSQL('attach "d:\src.db3" as source');
    LiteConnection.ExecSQL('insert into main.t_test select * from source.t_test');
    LiteQuery := TLiteQuery.Create(nil);
    try
      LiteQuery.Connection := LiteConnection;
      LiteQuery.SQL.Text := 'select * from main.t_test';
      LiteQuery.Open;
      while not LiteQuery.Eof do begin
        WriteLn(LiteQuery.FieldByName('f_id').AsString);
        LiteQuery.Next;
      end;
    finally
      LiteQuery.Free;
    end;
  finally
    LiteConnection.Free;
    readln;
  end;
end.

jodyatfsec
Posts: 8
Joined: Wed 06 Nov 2013 13:41

Re: Copying a SQLITE table from one database to another database

Post by jodyatfsec » Tue 05 May 2015 16:30

Thanks for the very quick response. This is just what I needed.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Copying a SQLITE table from one database to another database

Post by AlexP » Wed 06 May 2015 04:07

You are welcome. Feel free to contact us if you have any further questions.

Post Reply