简体   繁体   中英

Vanilla MySQL access from Ruby 1.9 on Snow Leopard

I am running Ruby 1.9 (ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin10]) on Slow Leopard (installed via MacPorts).

I then installed the Ruby MySQL client library via MacPorts: install rb19-mysql

Trying to use it I get the following error:

db.rb:4:in `initialize': wrong number of arguments(4 for 0) (ArgumentError)
    from db.rb:4:in `new'
    from db.rb:4:in `'

My code:

require 'mysql'
require 'pp'

dbh = Mysql.new("localhost", "testuser", "testpass", "test")
puts "Server version: " + dbh.get_server_info

It seems like I am missing something very basic here.

Did I install the right client library? I am using it correctly? Am I missing some other dependencies?

Would appreciate if someone could point me in the right direction.

Thanks!

你在寻找的是:

dbh = Mysql.real_connect("localhost", "testuser", "testpass", "test")

I have never written a line of Ruby in my life so maybe I'm going to embarrass myself, but don't you have to initialize Mysql first? I'm quite sure the fact that the function expects 0 parameters means it doesn't exist yet.

In the test file of the package you mention (you got me curious), I see the following line:

 assert_nothing_raised{@m = Mysql.init} 

your syntax seems to be correct, however. From the same test file

assert_nothing_raised{@m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)}

I would assume you need to do a Mysql.init() first.

If I'm wrong, let me know and I'll remove the answer.

Your code looks solid to me; it matches the examples given on the rb19-mysql homepage very well.

my = Mysql.new(hostname, username, password, databasename)
st = my.prepare("insert into tblname (col1,col2,col3) values (?,?,?)")
st.execute("abc",123,Time.now)
st.prepare("select col1,col2,col3 from tblname")
st.execute
st.fetch  # => ["abc", 123, #<Mysql::Time:2005-07-24 23:52:55>]
st.close

Which would seem to indicate that there's probably something not quite right with the port install. There were all kinds of issues with MacPorts not working properly after snow leopard upgrades - are you falling victim to this? I had to rebuild all my ports before they worked properly.

Also (and I know this is like religion to some people), you might want to consider ditching the MacPort version and just grab the MySql gem. For whatever reason, using the gems has been a far more pleasurable experience for me.

Hope that helps - good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM