views:

278

answers:

3

Hi,

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

A: 

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.

Topher Fangio
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?
Andrew
+4  A: 

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

Jirapong
Great! Thanks!I had libmysql problems before, but copied it from my Mysql installation - not from rubyforge. It works! YEA!
Andrew
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.
Sixty4Bit
A: 

Oh my God Jirapong Thank you very much mate, you saved me days of work with your dll solution. I was getting this error since days and have no idea how to solve. Thanks A LOT man !

Marco