Hi!
I want to use tables from multiple database.
Idea from here:
linkThese databases table structure is all the same.
See this code:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Sqlite3DS, db, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, DBGrids, Clipbrd;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Sqlite3Dataset1: TSqlite3Dataset;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
with Sqlite3Dataset1 do
begin
FileName:='db1.db';
TableName:='table1';
PrimaryKey:='Num';
ExecSQL('ATTACH "db2.db" AS "db_test";');
SQL:='SELECT * FROM '+
'main.'+TableName+' a '+
'inner join '+
'db_test.'+TableName+' b '+
'on b.'+PrimaryKey+' = a.'+PrimaryKey+' LIMIT 20;';
ShowMessage(SQL);
Clipboard.AsText:=SQL;
Open;
end;
end;
end.
I get error message (about):
EDatabase error:
duplicate name 'Num' in TFields
But, If I run these commands from commend shell (sqlite3.exe) see Clipboard.AsText:=SQL line,
then run perfectly, without error!
See:
.open db1.db
ATTACH "db2.db" AS "db_test";
.database
.table
SELECT * FROM main.table1 a inner join db_test.table1 b on b.Num = a.Num LIMIT 20;
sqlite> .table
db_test.table1 table1
sqlite> SELECT * FROM main.table1 a inner join db_test.table1 b on b.Num = a.Num LIMIT 20;
1|test 1|1|test 10
2|test 2|2|test 11
3|test 3|3|test 12
sqlite>
and run perfectly in sqlite3 shell...
Full demo project is attached with sample db files.Lazarus is 1.4.4
TSQLite3Dataset is the latest SVN
Thanks!