lks128
lks128

Reputation: 986

Multiple MySQL queries with Ruby

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

Upvotes: 5

Views: 1196

Answers (2)

Jirapong
Jirapong

Reputation: 24256

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

Upvotes: 7

Topher Fangio
Topher Fangio

Reputation: 20667

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.

Upvotes: 0

Related Questions