Forum: Ruby on Rails How to upload data using huge .sql file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B2d23c5b0b6de413d99702767d1d148d?d=identicon&s=25 Hitesh Rawal (hitesh)
on 2009-02-08 13:45
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
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2009-02-08 14:32
(Received via mailing list)
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 ]
for
much speedier loading.

Regards,
Craig
1128ccdc6b414a39e38f00acca680b8d?d=identicon&s=25 stretch (Guest)
on 2009-02-08 15:06
(Received via mailing list)
Hey Hitesh,

You could also use Heroku's yml data plugin

http://blog.heroku.com/archives/2007/11/23/yamldb_...

Al
B2d23c5b0b6de413d99702767d1d148d?d=identicon&s=25 Hitesh Rawal (hitesh)
on 2009-02-09 09:11
Craig Demyanovich 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 ]
> 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
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-02-09 09:26
(Received via mailing list)
maybe you got your 'max_allowed_packet' set too low in your database
config. fill in a bigger value and try again.
B2d23c5b0b6de413d99702767d1d148d?d=identicon&s=25 Hitesh Rawal (hitesh)
on 2009-02-09 10:02
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
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-02-09 10:39
(Received via mailing list)
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.
E60b2dc57668b5662ce3f07781e41710?d=identicon&s=25 Matthew Rudy Jacobs (matthewrudy)
on 2009-02-09 17:39
(Received via mailing list)
On Feb 9, 8:11 am, Hitesh Rawal <rails-mailing-l...@andreas-s.net>
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
B2d23c5b0b6de413d99702767d1d148d?d=identicon&s=25 Hitesh Rawal (hitesh)
on 2009-02-09 18:32
Matthew Rudy Jacobs wrote:
> On Feb 9, 8:11�am, Hitesh Rawal <rails-mailing-l...@andreas-s.net>
> 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
This topic is locked and can not be replied to.