Dbi::Interfaceerror: Could Not Load Driver (Uninitialized Constant MySQL error)

Ruby gem failure to load - Unable to load driver 'MySQL'

Solved the problem.

The core failure is bad code on Ruby 2.1.0 itself.
See here to fix Ruby itself. [DBI Row / delegate behavior between ruby 1.8.7 and 2.1

Once I started getting an error, I changed the DBI call of Mysql to MySQL hoping to fix it, but that just caused another problem.

Connect Ruby and MySQL

You're using extremely outdated and abandoned tools. The mysql gem has not seen a new release since 2013 and dbd-mysql has not been updated since 2010. Its highly questionable if they will work with modern versions of Ruby and MySQL.

What you want is the the mysql2 gem which is actively maintained.

# https://bundler.io/guides/bundler_in_a_single_file_ruby_script.html
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'mysql2', '~> 0.5.2'
end

client = Mysql2::Client.new(host: "localhost", username: "root")
results = client.query("SELECT * FROM users")
results.each do |row|
# do something with row, it's ready to rock
end

How to pull data from remote server database in rails?

You're not understanding how database access works.

We use a driver to talk to a database. The database vendors have different protocols that are used to connect, so a driver handles that.

Above that layer we'd have an API that talks to the driver. That'd be something like DBI, which knows how to talk to different drivers. We still have to write using the query language of the database but DBI gives us some advantages. It's a pain to convert from one database to another, because usually all the queries change and the inconsistencies between "standards" show up.

Above that layer we'd have something like ActiveRecord or Sequel, which are ORMs, and are mostly DBM agnostic. They allow us to use a consistent language to define our connections to databases, create queries and handle interactions. If we want to talk to a different database manager, we install the driver, change the connection string, and the rest should work.

This is a huge time savings and a "very good thing". You can use SQLite for your proof-of-concepts, and something like PostgreSQL, MySQL, or Oracle for your production system, without changing queries. Only the DSN/connection string changes usually.

Read through Sequel's "Connecting to a database" document to get an idea what ORMs can do, along with "Sequel: The Database Toolkit for Ruby" and "Cheat Sheet" for an idea what Sequel can do.

Could not find gem 'mocha (= 0.12.3) x86-mingw32'

Try installing it with the same exact version:

gem install mocha -v '0.12.3'


Related Topics



Leave a reply



Submit