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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.