code_junkie

How do I import a whitespace-delimited text file into MySQL?

Nov 11th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. 1 1 0.008 0 0 0 0 0
  2.  
  3. LOAD DATA INFILE 'filename' INTO TABLE mytable FIELDS TERMINATED BY ' ';
  4.  
  5. sed 's/ +/ /g' thefile > thefile.new
  6.  
  7. <?php
  8.  
  9. $db = mysql_connect('host', 'user', 'password')
  10. or die('Failed to connect');
  11. mysql_select_db('database', $db);
  12.  
  13. $fileHandle= @fopen("import.file", "r");
  14. if ($fileHandle) {
  15. while (!feof($fileHandle)) {
  16. $rawLine = fgets($fileHandle, 4096);
  17.  
  18. $columns = preg_split("/s+/", $rawLine);
  19.  
  20. //Construct and run an INSERT statement here ...
  21.  
  22. }
  23. fclose($fileHandle);
  24. }
  25.  
  26. ?>
  27.  
  28. LOAD DATA INFILE 'filename' INTO TABLE mytable FIELDS TERMINATED BY ',';
Add Comment
Please, Sign In to add comment