Subject Re: [IBO] update from one database to another
Author Svein Erling Tysvær
>the trouble is that I am lazy!

That's a virtue for a programmer - and a good reason for using Delphi.
Enclosed you'll find (but hopefully not the list) Delphi source files (D5)
for a program I wrote to transfer records from a table in one database to a
similar table in another database (the target table must have at least the
same fields included, but can have additional fields). I do use a
"home-grown" component for TIB_Connections, but just replace the two
instances with an ordinary TIB_Connection and replace DatabaseNameCustom
with DatabaseName. Then you can fiddle around to make it do whatever you
want. I recommend you do not use it as is - unless your users speak
Norwegian pretty well! Sorry for the Norwegian naming of variables...

You'll notice that I didn't follow my own advice and used a TIB_Cursor
where I ought to have used a TIB_DSQL. Guess I was a bit tired the day I
wrote that utility.

Set
----------

object fMain: TfMain
Left = 304
Top = 106
Width = 359
Height = 270
Caption = 'Kopier tabellinnhold'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object sbMain: TStatusBar
Left = 0
Top = 224
Width = 351
Height = 19
Panels = <
item
Alignment = taCenter
Width = 200
end
item
Alignment = taCenter
Width = 50
end>
SimplePanel = False
end
object ibgTables: TIB_Grid
Left = 0
Top = 65
Width = 351
Height = 159
CustomGlyphsSupplied = []
DataSource = ibdSrc
Align = alClient
ReadOnly = True
TabOrder = 1
DefaultRowHeight = 17
end
object Panel1: TPanel
Left = 0
Top = 0
Width = 351
Height = 65
Align = alTop
TabOrder = 2
object Label1: TLabel
Left = 8
Top = 40
Width = 61
Height = 13
Caption = 'Måldatabase'
end
object Label2: TLabel
Left = 8
Top = 16
Width = 67
Height = 13
Caption = 'Kildedatabase'
end
object eSrc: TEdit
Left = 80
Top = 8
Width = 185
Height = 21
TabOrder = 0
end
object eTrg: TEdit
Left = 80
Top = 32
Width = 185
Height = 21
TabOrder = 1
end
object bbTabeller: TBitBtn
Left = 270
Top = 8
Width = 75
Height = 25
Caption = 'Tabeller'
TabOrder = 2
OnClick = bbTabellerClick
Kind = bkOK
end
object bbOverfor: TBitBtn
Left = 270
Top = 32
Width = 75
Height = 25
Caption = 'Overfør'
TabOrder = 3
OnClick = bbOverforClick
Kind = bkOK
end
end
object cnSrc: TKrgIB_Connection
LoginUsernamePrefix = 'STY'
Params.Strings = (
'CHARACTER SET=ISO8859_1'
'SERVER=KRG_SRV3'
'PATH=F:\hoveddatabase\test.gdb'
'PROTOCOL=TCP/IP')
Left = 8
Top = 104
end
object tsSrc: TIB_Transaction
IB_Connection = cnSrc
ReadOnly = True
Isolation = tiConcurrency
RecVersion = False
Left = 40
Top = 104
end
object qSrcTbl: TIB_Query
DatabaseName = 'KRG_SRV3:F:\hoveddatabase\test.gdb'
FieldsVisible.Strings = (
'DB_KEY=FALSE')
IB_Connection = cnSrc
IB_Transaction = tsSrc
SQL.Strings = (
'SELECT RDB$RELATION_NAME'
'FROM RDB$RELATIONS'
'WHERE RDB$RELATION_NAME NOT STARTING '#39'RDB$'#39
'ORDER BY RDB$RELATION_NAME')
ColorScheme = False
MasterSearchFlags = [msfOpenMasterOnOpen, msfSearchAppliesToMasterOnly]
ReadOnly = True
BufferSynchroFlags = []
FetchWholeRows = True
Left = 72
Top = 80
end
object cnTrg: TKrgIB_Connection
Params.Strings = (
'CHARACTER SET=ISO8859_1')
Left = 8
Top = 176
end
object tsTrg: TIB_Transaction
IB_Connection = cnTrg
Isolation = tiConcurrency
Left = 48
Top = 176
end
object cTrg: TIB_Cursor
IB_Connection = cnTrg
IB_Transaction = tsTrg
ColorScheme = False
MasterSearchFlags = [msfOpenMasterOnOpen, msfSearchAppliesToMasterOnly]
Left = 88
Top = 176
end
object cSrc: TIB_Cursor
DatabaseName = 'KRG_SRV3:F:\hoveddatabase\test.gdb'
IB_Connection = cnSrc
IB_Transaction = tsSrc
SQL.Strings = (
'select * from <table>')
ColorScheme = False
MasterSearchFlags = [msfOpenMasterOnOpen, msfSearchAppliesToMasterOnly]
ReadOnly = True
Left = 88
Top = 128
end
object ibdSrc: TIB_DataSource
Dataset = qSrcTbl
Left = 112
Top = 80
end
end

----------

unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, IB_Components, KrgIB_Connection, StdCtrls, Buttons, Grids,
IB_Grid, ExtCtrls;

type
TfMain = class(TForm)
cnSrc: TKrgIB_Connection;
tsSrc: TIB_Transaction;
qSrcTbl: TIB_Query;
cnTrg: TKrgIB_Connection;
tsTrg: TIB_Transaction;
cTrg: TIB_Cursor;
cSrc: TIB_Cursor;
sbMain: TStatusBar;
ibdSrc: TIB_DataSource;
ibgTables: TIB_Grid;
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
eSrc: TEdit;
eTrg: TEdit;
bbTabeller: TBitBtn;
bbOverfor: TBitBtn;
procedure bbOverforClick(Sender: TObject);
procedure bbTabellerClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
fMain: TfMain;

implementation

{$R *.DFM}
var
Avbryt: Boolean;

procedure TfMain.bbOverforClick(Sender: TObject);
var
FeltNavn : TStringList;
Tell,
Poster,
AntallFelt: Integer;
begin
cnSrc.BeginBusy(True);
cnTrg.BeginBusy(True);
if not cnSrc.Connected then begin
sbMain.Panels[0].Text:='Kan ikke overføre før du har valgt tabell(er)';
Exit;
end;
cnTrg.Username:=cnSrc.Username;
cnTrg.Password:=cnSrc.Password;
cnTrg.DatabaseNameCustom:=eTrg.Text;
cnTrg.Connect;
if not cnTrg.Connected then begin
sbMain.Panels[0].Text:='Klarte ikke logge på måldatabasen';
Exit;
end;
try
qSrcTbl.First;
repeat
if qSrcTbl.selected[qSrcTbl.RowNum] then begin
sbMain.Panels[0].Text:=qSrcTbl.Fields[0].AsString;
sbMain.Panels[1].Text:='';
Application.ProcessMessages;
cSrc.SQL.Clear;
cSrc.SQL.Add('Select * from '+qSrcTbl.Fields[0].AsString);
cSrc.Prepare;
FeltNavn:=TStringList.Create;
try
cSrc.GetFieldNamesList(FeltNavn);
AntallFelt:=FeltNavn.Count;
cTrg.SQL.Clear;
cTrg.SQL.Add('Insert into '+qSrcTbl.Fields[0].AsString+' (');
for Tell:=0 to AntallFelt - 2 do
cTrg.SQL.Add(FeltNavn[Tell]+', ');
cTrg.SQL.Add(FeltNavn[AntallFelt - 1]+') ');
cTrg.SQL.Add('Values (');
for Tell:=0 to AntallFelt - 2 do
cTrg.SQL.Add(':'+FeltNavn[Tell]+', ');
cTrg.SQL.Add(':'+FeltNavn[AntallFelt - 1]+') ');
finally
FeltNavn.Free;
end;
cTrg.Prepare;
cSrc.Open;
Poster:=0;
while not cSrc.Eof or Avbryt do begin
inc(Poster);
if Poster MOD 100 = 0 then begin
sbMain.Panels[1].Text:=inttostr(Poster);
Application.ProcessMessages;
end;
if Poster MOD 10000 = 9999 then
tsTrg.Commit;
for Tell:=0 to AntallFelt - 1 do
if cSrc.fields[Tell].IsNull then
cTrg.Params[Tell].Clear
else
cTrg.Params[Tell].AsVariant:=cSrc.fields[Tell].AsVariant;
cTrg.Execute;
cSrc.Next;
end;
cSrc.Close;
cSrc.Unprepare;
cTrg.Unprepare;
if tsTrg.TransactionIsActive then
tsTrg.Commit;
end;
qSrcTbl.Next;
until qSrcTbl.Eof or Avbryt;
qSrcTbl.Close;
if tsSrc.TransactionIsActive then
tsSrc.Commit;
finally
cnTrg.Disconnect;
cnSrc.EndBusy;
cnTrg.EndBusy;
end;
sbMain.Panels[1].Text:='Ferdig';
end;

procedure TfMain.bbTabellerClick(Sender: TObject);
begin
if tsSrc.TransactionIsActive then
tsSrc.Commit;
if cnSrc.Connected then
cnSrc.Disconnect;
cnSrc.DatabaseNameCustom:=eSrc.Text;
cnSrc.Login;
if cnSrc.Connected then
qSrcTbl.Open;
end;

end.

----------

program Overfor;

uses
Forms,
main in 'main.pas' {fMain};

{$R *.RES}

begin
Application.Initialize;
Application.CreateForm(TfMain, fMain);
Application.Run;
end.


[Non-text portions of this message have been removed]