Jump to content

parsing a large file and inserting content into MySQL


obyno

Recommended Posts

I am trying to read the contents of a forty megabyte text file that was saved as a Tab delimited file from MS Excel. The intention is to insert the columns within into a MySQL table. The trouble is that the maximum times in minutes which a file can be executed before php gives up is not long enough to compare with the lenth of time thi is taking. Can somebody please suggest to me how I might accomplish this? Thanks in advance
Have a look at [url=http://dev.mysql.com/doc/refman/5.0/en/load-data.html]LOAD DATA INFILE[/url]


Simple example...

[code=php:0]<?

define ( 'DB_HOST', 'localhost:3306' );    // should not need to change
define ( 'DB_USER', 'user' );              // enter the user name for this database
define ( 'DB_PASS', 'pass' );              // enter the password for this database
define ( 'DB_NAME', 'my_db' );              // enter the database name you are connecting to


define ( 'DB_TABLE', 'my_table' );          // enter the NAME of the database TABLE to INSERT data into
define ( 'DBF_CSVP', './path_to/csv.txt' ); // enter the PATH and NAME of the CSV FILE to IMPORT

mysql_connect ( DB_HOST, DB_USER, DB_PASS ) or die ( 'Connection Error: ' . mysql_error () );
mysql_select_db ( DB_NAME ) or die ( 'Select DB (' . DB_NAME . ') Error: ' . mysql_error () );
mysql_query ( "LOAD DATA INFILE '" . DBF_CSVP . "' INTO TABLE " . DB_TABLE . " FIELDS TERMINATED BY '\\t\\t' OPTIONALLY ENCLOSED BY '' ESCAPED BY '\\' LINES TERMINATED BY '\\r\\n'" );

?>[/code]


me

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.