Mysqldump and ruby

U

Uros

Hello dear ruby users,

another problem arose in my first ruby programm. I have this two
mysqldump files that I need to send to the mysql server so the
databases get populated. I thought it's gonna be an easy solution with
a do loop just cycling through the lines and sending them to the
mysql.query method.

But then I remembered that the .sql dump files are not one query per
line type. Any ideas how can I make it work? Do I need a routine that
parses through a .sql file and creates queries or how can it be done?

Thanks in advance for your time and answers,

Uros
 
O

OliverMarchand

You are being to unspecific about what needs to be done. Most people,
including me, do not know what the content of a .sql file is. In
general it should be immaterial whether the data to be written to the
database is given on one line or not.

Oliver
 
U

Uros

Ups, yeah, didn't think of that,sorry. Basicaly a .sql file is a file
that is populated with data from the mysqldump command. It's a text
file with SQL statements.

I was thinking of doing it like this:

file.open("mysql.sql") do |io|
io.each_line do |line|
my.query(line)

...but then I thought that this is not going to work, since the sql
statements are not all in one line in the .sql file.

I hope I made my question a little bit clearer now, sorry for the
inconviniance.

Best regards,

Uros
 
O

OliverMarchand

One way to do this without .each_line is e.g. to read the whole file
into memory and then divide into records as needed. Something along the
line (not tested):

content = File.read("mysql.sql")
content = content.split(<some delimiting string>)
content.each do |entry|
q = <convert entry in suitable format for a query>
my.query(q)
end

You could also use each_line and appen to a tring as needed:

query = ""
....each_line do |line|
if <is a new record>
my.query(query)
else
query += line
end
end


Just go ahead, write some code. Put debugging statements in, check the
results and so on until the code does what it should do. Then sit back
and look at your code. Are you satisfied? If there are small changes
that make things much clearer, go ahead and make them. Best use a
versioning system like CVS, such that you are less afraid of making
changes.

cheers,
Oliver

or maybe like this
 
U

Uros

Thanks for your help.

After trying some stuff, I stumbled upon IO.popen and thought I'd use
that. It's easier for me to use popen, since my knowledge really isn't
helping me to build some fancy string delimitier/parser/togethersticker
function.. ;-) I think popen will get the job done..
Thanks anyway...

Best regards,

uros
 
T

tloring

Any reason you can't, or don't want to, simply use mysql itself to load
your dump files?

mysql -u user --password=passwd --database=dbname < dump.sql
 
T

tloring

Any reason you can't, or don't want to, simply use mysql itself to load
your dump files?

mysql -u user --password=passwd --database=dbname < dump.sql
 
U

Uros

Well, yes, I'm using this method like this
mysql_string = "mysql -u drgflyuser -p****** dragonfly_"+i.to_s+" <
c:/ruby-devel/dragonfly_1_vanilla_installed.sql"
IO.popen(mysql_string)

I need to do it from a script so it's all automated. But it is not
looking like it is working properly, since the databases get only 1/4
populated. It looks like the sql statements break somwhere, but when I
do it manualy averything is fine. Any ideas anyone?

br,
uros
 
T

tloring

If it works fine manually, how about command expression or system call?

`#{mysql_string}`

or

%x{#{mysql_string}}

or

system(mysql_string)

Perhaps w/ IO.popen there is some sort of buffering issue? You
shouldn't have a problem like that w/ the command expansion or system
call.

thom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,070
Members
47,676
Latest member
scazeho

Latest Threads

Top