[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Mysqldump and ruby

Uros

4/13/2006 7:04:00 AM

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

8 Answers

OliverMarchand

4/13/2006 7:31:00 AM

0

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

Uros

4/13/2006 8:23:00 AM

0

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

OliverMarchand

4/13/2006 8:45:00 AM

0

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

Uros

4/13/2006 10:09:00 AM

0

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

tloring

4/13/2006 8:59:00 PM

0

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

tloring

4/13/2006 8:59:00 PM

0

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

Uros

4/14/2006 6:34:00 AM

0

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

tloring

4/14/2006 12:38:00 PM

0

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