473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Insert 1 million rows from text file into MySQL database

Basically, I need to insert a million rows into a mysql table from a .txt file. The "easiest" way I can think about doing this is loading the file into an AIR application and have it strip down into 1000 workable .txt files of 1000 lines. Then after that is done, use the app to upload each file one after the other in a loop. Can anyone tell me if there is an easier, more efficient way of doing this. I really don't want to have to go through that whole building process. Thanks in advance!!
Mar 18 '10 #1
8 8319
Dormilich
8,658 Expert Mod 8TB
I’d try to read the file linewise and use a Prepared Statement (i.e. submit one line after another) to write each entry.
Mar 18 '10 #2
Hi, thanks for the response. I have played around with prepared statements before. However, I'll be doing this online. Wouldn't I have to worry about PHP timing out? For the life of me, I have never been able to change the timeout of my PHP scripts. Thanks!!
Mar 18 '10 #3
Atli
5,058 Expert 4TB
@fishnfrogs
Hey. Try set_time_limit(0).

If you can set an unlimited time-limit, then Dormilich's suggestion is also what I would recommend. A lot "cleaner" than all the other alternatives I can think of.
Mar 18 '10 #4
I'll give it a try and post back. THANKS!!!!!
Mar 18 '10 #5
mshmyob
904 Expert 512MB
If you don't need PHP to do it then you could just use the MySQL load data command line.

Expand|Select|Wrap|Line Numbers
  1. load data local infile 'c:\\yourfile.txt' into table YourDataBaseName.YourTableName fields terminated by ",";
  2.  
You would change the "," to whatever seperates your field names.
If your records are on each line then that will work - if your records are all wrapped then you can add something like records terminiated by "#" after the fields terminater.

cheers,
Mar 19 '10 #6
Hi, I'm running into a 500 Internal Server Error. I get about 50 to 80k in, and the server throws that error. Here is the code I'm currently using:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. set_time_limit(0);
  4. ini_set("max_execution_time", 0);
  5.  
  6. require_once 'database_creds.php';
  7.  
  8. $mysqli = new mysqli($hostname, $username, $password, $database);
  9. if (mysqli_connect_errno())
  10. {
  11.       printf("Connect failed: %s\n", mysqli_connect_error());
  12.        exit();
  13. }
  14. if($stmt = $mysqli->prepare("INSERT INTO table (id, value) VALUES ('', ?)"))
  15. {
  16.     $fp = fopen('file.txt', 'r');
  17.     echo "Starting...\n";
  18.     while(!feof($fp))
  19.     {
  20.         $line = fgets($fp);
  21.         $stmt->bind_param('s', $line);
  22.         $stmt->execute();
  23.     }
  24.     $stmt->close();
  25.     $result = $mysqli->close();
  26.     if($result)
  27.     {
  28.         echo 'CLOSED';
  29.     }
  30. }
  31.  
  32. ?>
  33.  
Also, I'm using Firefox 3 to load the page. Is there something more I can do or does this have to do with my hosting company? Thanks!!
Mar 22 '10 #7
Question why not use the file function and a foreach loop? Also I'm guessing the ID shouldn't be submitted as a empty string going on the practice that all ID fields are auto_incremented integers.

Your if statement isn't doing any real error checking its simply checking if the variable can be set to something always true unless the function does not return a value.

Expand|Select|Wrap|Line Numbers
  1. if( ( $stmt = $mysqli->prepare("INSERT INTO table (id, value) VALUES ('', ?)") ) != FALSE )
  2.  
Mar 22 '10 #8
The issues aren't coming from loading the file or the query string. All those seem to work fine. I'm guessing it's something else. I also tried making one long query string, but I either got the 500 error or the packet was too big for mysql. Then I tried to break it up into smaller arrays, and use a counting system and do smaller queries. I only got to 500k before my friend the 500 internal server error came calling again. *Sigh* It looks like I'll be going the AIR route again. Thanks again for your help!!
Mar 22 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: atse | last post by:
Hi, I am still stick on the text file being imported to database. Can anybody help? I have just done the csv format files. And then I work on text and DAT formats, but I have problem to import...
2
by: bienwell | last post by:
Hi all, Do you have any source code to import data from Excel file or text file into database in ASP.NET program ? Please give me your reference if you have. Thanks in advance
7
by: sanjay_scjp | last post by:
Hi, I want to save a file into databse how can it is possible ? Can any one help me to make codes in jsp. the codes require for upload a text file into database Rgds SanJay
0
by: lipsa | last post by:
i want 2 write arnd 2 lakh rows of data(16 columns)data in a pipe separated text file from database(sql server) .(its the need ,cant help) i m using recordset/file system object. its taking a...
9
by: asenthil | last post by:
Hai to all,, i had to tried to retrive and write a single row to a file from the database.. But dont know to write all the retrived rows to a file from the database.. how to do that... ...
2
by: Zarwadi | last post by:
How do you count the number of rows in a mysql database. could anyone help
6
by: ssharpjr | last post by:
Hi Guys, I'm new to Python (mostly) and I'm wanting to use it for a new project I'm faced with. I have a machine (PLC) that is dumping its test results into a fixed- length text file. I need...
0
by: Edwin.Madari | last post by:
here is a working code snippet to read from MySQL db. python tutorial has examples of reading from files. put them together to do your task. ===================================================...
3
by: puneetmca | last post by:
hi.. i m trying to write data from txt file onto database but when i am executing my php code for it the empty record will be inserted in the database not the data written on txt file will be...
2
by: Nilanjan Sanyal | last post by:
how to retrieve hindi text from mysql database using php
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.