Exporting and Importing Raw Data
Go Up to Importing and Exporting Data
Use the TIBSQL
component, along with the TIBOutputRawFile
and TIBInputRawFile
objects to perform batch imports and exports of raw data. A raw file is the equivalent of InterBase external file output. Raw files are not limited to straight character format, so whatever structure is defined by your query is what goes in the file.
Use a SQL SELECT
statement to export the data to the raw file, and an INSERT
statement to import the raw data into another database.
Raw files are probably the fastest way, aside from external tables, to get data in and out of an InterBase database, although dealing with fixed-width files requires considerable attention to detail.
Exporting Raw Data
To export raw data, you will need TIBSQL
, TIBDatabase
, and TIBTransaction
components. Associate the components with each other, select a source database, and set the connections to active.
Use the Database Editor to set up the database component. To start the Database Editor, right click the database component with the mouse and select Database Editor from the drop-down menu.
The following code snippet outputs selected data with a SQLSELECT
statement from the SOURCE
table to the file source_raw
.
procedure TForm1.Button1Click(Sender: TObject);
var
RawOutput : TIBOutputRawFile;
begin
IBSQL1.SQL.Text := 'Select name, number, hired from Source';
RawOutput := TIBOutputRawFile.Create;
try
RawOutput.Filename := 'source_raw';
IBSQL1.BatchOutput(RawOutput);
finally
RawOutput.Free;
end;
end;
Importing Raw Data
To import raw data, you will need TIBSQL
, TIBDatabase
, and TIBTransaction
components. Associate the components with each other, select a destination database, and set the connections to active.
Use the Database Editor to set up the database component. To start the Database Editor, right click the database component with the mouse and select Database Editor from the drop-down menu.
It is important to note that you must import data into a table with the same column definitions and data types, and in the same order; otherwise, all sorts of unpredictable and undesirable results may occur.
The following code snippet inputs selected data with an SQL INSERT
statement from the source_raw
file created in the last example into the DESTINATION
table.
procedure TForm1.Button2Click(Sender: TObject);
var
RawInput : TIBInputRawFile;
begin
IBSQL2.SQL.Text := 'Insert into Destination values(:name, :number, :hired)';
RawInput := TIBInputRawFile.Create;
try
RawInput.Filename := 'source_raw';
IBSQL2.BatchInput(RawInput);
finally
RawInput.Free;
end;