# How to upload data using huge .sql file

**URL:** <https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763>\
**Category:** Rails\
**Created:** [February 8, 2009, 1:45pm UTC](https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763 "2009-02-08T13:45:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [February 8, 2009, 1:45pm UTC](https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763/1 "2009-02-08T13:45:48Z")

</div>

Hi,

I am using Ruby - 1.8.6 and Rails- 1.2.6.  
When I trying to upload data using the .sql file through my code

I got “Mysql::Error: Lost connection to MySQL server during query:  
rollback;”

File size is 6 MB.

The code I have used is like:

numberOfRecord =  
ActiveRecord::Base.connection.update(File.open(fileName).read)

Please let me know how I upload data using huge .sql file.

If anyone has any possible solutions or help on this problem it would be  
appreciated very much.

Thank you

---

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [February 8, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763/2 "2009-02-08T14:32:34Z")

</div>

To bulk-load a database, I’d use the tools provided by the database.  
With  
mysql, you’d do something like this (assuming that you’re logged in to  
the  
database server):

$ mysql -u [username] -p [database] \< [file]

For example:

$ mysql -u craig -p hockey\_development \< players.sql

The -p flag will tell mysql to prompt me for a password, since I didn’t  
include one right after it so I don’t have to enter it in plain text. It  
doesn’t show any progress. When it’s done, you’ll be back at the command  
prompt.

If for some reason you really need to do this through Rails, I recommend  
looking at ar-extensions [[http://continuousthinking.com/tags/arext](http://continuousthinking.com/tags/arext) ]  
for  
much speedier loading.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [February 8, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763/3 "2009-02-08T15:06:03Z")

</div>

Hey Hitesh,

You could also use Heroku’s yml data plugin

> **[YamlDb for Database-Independent Data Dumps](https://blog.heroku.com/yamldb_for_databaseindependent_data_dumps)**
>
> One of the many benefits of Rails is database independence. Migrations are particularly nice in this regard; and the easy-to-read / Rubyified display of yo

Al

---

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [February 9, 2009, 9:11am UTC](https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763/4 "2009-02-09T09:11:14Z")

</div>

Craig D. wrote:

> To bulk-load a database, I’d use the tools provided by the database.  
> With  
> mysql, you’d do something like this (assuming that you’re logged in to  
> the  
> database server):
> 
> $ mysql -u [username] -p [database] \< [file]
> 
> For example:
> 
> $ mysql -u craig -p hockey\_development \< players.sql
> 
> The -p flag will tell mysql to prompt me for a password, since I didn’t  
> include one right after it so I don’t have to enter it in plain text. It  
> doesn’t show any progress. When it’s done, you’ll be back at the command  
> prompt.
> 
> If for some reason you really need to do this through Rails, I recommend  
> looking at ar-extensions [[http://continuousthinking.com/tags/arext](http://continuousthinking.com/tags/arext) ]  
> for  
> much speedier loading.
> 
> Regards,  
> Craig

Hi Craig,

Thanks for your response.

But I want to upload sql file using ROR.  
I observed that the problem with ruby statement which I have write.

numberOfRecord =  
ActiveRecord::Base.connection.update(File.open(fileName).read)

I think the update method has some limitation, that’s y I am not able to  
upload huge sql file. It works fine with small size of sql file if file  
size is \< 900 kb.

Thanks  
Hitesh

---

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [February 9, 2009, 9:26am UTC](https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763/5 "2009-02-09T09:26:27Z")

</div>

maybe you got your ‘max\_allowed\_packet’ set too low in your database  
config. fill in a bigger value and try again.

---

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [February 9, 2009, 10:02am UTC](https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763/6 "2009-02-09T10:02:09Z")

</div>

MaD wrote:

> maybe you got your ‘max\_allowed\_packet’ set too low in your database  
> config. fill in a bigger value and try again.

Thanks,

It is working now I am able to upload sql file.  
But facing another problem.  
My file has multiple INSERT statement with multiple values during  
uploading It gives me error

Error: #42000You have an error in your SQL syntax; check the manual that  
corresponds to your MySQL server version for the right syntax…

If I upload data using same file via command prompt on sql it works.

Could you please help on this.

Thanks For your response.

Hitesh

---

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [February 9, 2009, 5:39pm UTC](https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763/7 "2009-02-09T17:39:10Z")

</div>

On Feb 9, 8:11 am, Hitesh R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ActiveRecord::Base.connection.update()File.open(fileName.read)

One of the constraints of using ActiveRecord::Base.connection is that  
it can only do one statement at a time

if you try and pass in “first statement ; second statement” it will  
break.

you may find something like this works better.

sql = File.open(fileName).read

statements = sql.split(“;”)

count = 0  
statements.each do |sql\_statement|  
count += ActiveRecord::Base.connection.update(sql\_statement)  
end

---

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [February 9, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763/8 "2009-02-09T18:32:20Z")

</div>

Matthew R. Jacobs wrote:

> On Feb 9, 8:11ï¿½am, Hitesh R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ActiveRecord::Base.connection.update()File.open(fileName.read)
> 
> One of the constraints of using ActiveRecord::Base.connection is that  
> it can only do one statement at a time
> 
> if you try and pass in “first statement ; second statement” it will  
> break.
> 
> you may find something like this works better.
> 
> sql = File.open(fileName).read
> 
> statements = sql.split(“;”)
> 
> count = 0  
> statements.each do |sql\_statement|  
> count += ActiveRecord::Base.connection.update(sql\_statement)  
> end

Yes you are correct, I have used same approach to update my db records.

Thanks for your response.

But I dont want to use use this approach because not sure that,  
is ruby variable keep huge amount of data?  
I have tested with 10MB sql file and It works fine but don’t know what  
happen when the file size is more than 10MB.

Please let me know, If you have any Idea or Experience?

Thanks  
Hitesh

---

<div class="post-metadata">

**Author:** ![hitesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hitesh](https://www.ruby-forum.com/u/hitesh)\
**Post date:** [February 9, 2009, 10:39am UTC](https://www.ruby-forum.com/t/how-to-upload-data-using-huge-sql-file/159763/9 "2009-02-09T10:39:16Z")

</div>

hard to say. does it give you any more information about where and  
what went wrong? probably some problem with escaping or sth. like it.
