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.
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.
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.
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.
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?