5

I'm having troubles with MySQL queries in Ruby. I use 'mysql' gem.

Configuration is stored in a separate yml file and loaded into @conf variable. This is my code:

# connect to the database
Mysql::new(@conf['sql_host'], @conf['sql_user'], @conf['sql_password'], @conf['sql_base'])

# it's ok when we're doing this
my.query("SELECT * FROM `my_table`") do |e|
  # code
end

# Maybe, I've missed something here...

# really this query will insert value into other table, used SELECT just for testing
# this throws exception: 'query: not connected'
my.query("SELECT * FROM `my_table_2`")

Windows XP
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
mysql (2.8.1, 2.7.3)
MySQL client version: 5.0.51a

Second query throws 'query: not connected'.

2 Answers 2

7

First of all, your program looks ok. I am 100% sure you have libmysql.dll version problem.

I can reproduce this with libmysql.dll provided from Mysql installer.

Download this file and replace in c:\ruby\bin\

http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll

and re-run your program without any change.

Related issue and credit to here

Sign up to request clarification or add additional context in comments.

3 Comments

Great! Thanks! I had libmysql problems before, but copied it from my Mysql installation - not from rubyforge. It works! YEA!
Thanks Jirapong. Just to clarify the fix, a 5.0.x library will work. I am using 5.1.42 server and the 5.0.4 libmysql.dll in ruby/bin.
THANK YOU! I've been trying all day with some different ORM wrappers and finally gave up to use the Ruby Mysql directly, and ran into the same problem. Turned out to be the wrong version for me as well, this one solved it :-)
0

I have run into immeasurable problems when attempting to use Ruby's mysql gem on any version of Windows. From what I can tell, it simply doesn't work unless you can figure out how to compile it yourself (which is a royal pain in the ass).

Have you considered using ActiveRecord as your ORM (object relational mapping) layer and doing development with SQLite or some other database on Windows and then running the production environment on Linux with MySQL? This is the solution that I took a while back and it worked out quite well.

1 Comment

The main problem is that database is already done and running. I'm writing a small tool for this base. I just need to execute database queries. Is there any other way to do this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.