Subject Re: [firebird-support] Best way to import from a TextFile
Author Milan Babuskov
lec_sas wrote:
> Hi, I am jsut wondering what the best way to import data from a text
> file is.

What kind of text file? CSV, fixed length?

Here's a sample PHP code to import database from CVS file. I skipped the
error checking and data is separated by tabs, without quoting:

ibase_connect('path/to/database.fdb', 'sysdba', 'masterkey');
$fp = fopen('file.txt', 'rt');
while (!feof($fp))
$line = fgets($fp);
$data = explode("\t", $line);
foreach ($data as $d)
$ready[] = str_replace("'", "''", $d);
ibase_query('insert into table (...) values ('
.join(',', $ready).')');

You could speed it up by using prepared statements with ibase_prepare
and ibase_execute. For fixed-length use an array of lengths and substr
instead of explode.

Even with error checking it's 20-30 lines of code, and shouldn't take
more than half hour to make it work perfectly. I guess you already spent
that much time searching the Internet for solution.

Milan Babuskov