Subject | Re: [firebird-support] Best way to import from a TextFile |
---|---|
Author | Milan Babuskov |
Post date | 2006-10-10T20:59:42Z |
lec_sas wrote:
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).')');
}
fclose($fp);
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
http://swoes.blogspot.com/
http://www.flamerobin.org
> Hi, I am jsut wondering what the best way to import data from a textWhat kind of text file? CSV, fixed length?
> file is.
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).')');
}
fclose($fp);
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
http://swoes.blogspot.com/
http://www.flamerobin.org