SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Uk
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error connecting to Mysql...

    I am having trouble connecting to mysql database. I'm i missing something? This is my database connection file:

    Code:
    require "mysql2"
    
    # error trap
    begin
      dbh = Mysql2::new('127.0.0.1','root','','Accounts')
    rescue Exception => e
      puts "Unable to connect to database. Error: \"#{e}\""
      exit 1
    end
    Error thrown:

    Code:
    Unable to connect to database. Error: "undefined method `new' for Mysql2:Module"
    What's the best way to connect to mysql database in ruby... not rails... im working on com-line app?

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,116
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Have a look at the usage for that gem here.
    https://github.com/brianmario/mysql2

    Code ruby:
    client = Mysql2::Client.new(:host => "localhost", :username => "root")

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Uk
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yh had a look at it... looks very cool and neat - definitely way to go. And also found another way of doing it in case someone wants to experiment with other ways: http://ruby-dbi.rubyforge.org/rdoc/files/README.html

    Thanks again.

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Uk
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just had to post this:

    Code:
    # require necessary libs
    require "rubygems"
    require "mysql2"
    
    # error trap: exception handler
    begin
      @dbh = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "", :database => "dbname")
    rescue Exception => e
      puts "Unable to connect to database. Error: \"#{e}\""
      exit 1
    end
    works just fine .... Thank you so much @markbrown4, You're the man!

  5. #5
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,116
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    No problemo. Happy to help

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Uk
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just curious.. does this method also support prepared statements?

  7. #7
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,116
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    I use Rails exclusively so am only familiar with the things that ActiveRecord supports, so I had to go look up what prepared statements were

    Yes, the mysql2 module appears to support them.
    http://zetcode.com/db/mysqlrubytutorial/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •