Ruby on rails 连接mysql数据库实践

1.安装好railsinstaller后,先修改gem的源。

1.1查看源

gem sources

1.2移除原有的:https://rubygems.org,由于使用原有的源来用gem安装的时候,一直报连接失败的原因,我们改成淘宝的源。

gem sources -r https://rubygems.org

1.3添加gem的淘宝源

gem sources -a https://ruby.taobao.com

添加成功后,我们来安装mysql和mysql2

前提,本机已经安装了mysql-5.6.24-win32。在用gem安装mysql和mysql2,需要将配置路径指导本机mysql的安装根目录,我本机的mysql安装在:D:\software\MySql

所以安装mysql和mysql2的时候,使用如下命令:

# #mysql  
gem install mysql --platform=ruby -- --with-opt-dir=D:\software\MySql
# #mysql2  
gem install mysql2 --platform=ruby -- --with-mysql-dir=D:\software\MySql

安装成功后显示如下:

D:\>gem install mysql --platform=ruby -- --with-opt-dir=D:\software\MySql
Fetching: mysql-2.9.1.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions with: '--with-opt-dir=D:\software\MySql'
This could take a while...
Successfully installed mysql-2.9.1
Parsing documentation for mysql-2.9.1
Installing ri documentation for mysql-2.9.1
Done installing documentation for mysql after 0 seconds
1 gem installed


安装完成后,记得一定,要将,下图的两个文件(libmysql.dll,libmysql.lib)

Ruby on rails 连接mysql数据库实践_第1张图片

拷贝到ruby的根目录下下的bin目录里面,如下图:

Ruby on rails 连接mysql数据库实践_第2张图片


如果上面安装的时候,指定了其它版本的mysql源,在运行的时候,会报如下错误:

D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:
in `require': Incorrect MySQL client library version! 
This gem was compiled for 5.6.24 but the client library is 6.1.6. (RuntimeError)
        from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/mysql-2.9.1/lib/mysql.rb:6:in `rescue in '
        from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/mysql-2.9.1/lib/mysql.rb:2:in `'
        from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:135:in `require'
        from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
        from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:144:in `require'
        from a.rb:4:in `
'

切记,一定要配置成本机安装的mysql根路径哦,不要从网上下载其它版本。




然后用ruby编写如下命令:

# -*- coding: UTF-8 -*-
require "dbi"

require 'mysql'  

class Person

	def say
		
		# need dbi
		begin
		     # 连接到 MySQL 服务器
		     dbh = DBI.connect("DBI:Mysql:jucheap:localhost", 
		                       "root", "123")
		     # 获取服务器版本字符串,并显示
		     row = dbh.select_one("SELECT VERSION()")
		     puts "Server version: " + row[0]
		rescue DBI::DatabaseError => e
		     puts "An error occurred"
		     puts "Error code:    #{e.err}"
		     puts "Error message: #{e.errstr}"
		ensure
		     # 断开与服务器的连接
		     dbh.disconnect if dbh
		end

		
		#need mysql
	    begin  
	      db = Mysql.init  
	      db.options(Mysql::SET_CHARSET_NAME, 'utf8')  
	      db = Mysql.real_connect("127.0.0.1", "root", "123", "jucheap", 3306)  
	      db.query("SET NAMES utf8")
	      db.query("drop table if exists tb_test")  
	      db.query("create table tb_test (id int,text LONGTEXT) ENGINE=MyISAM DEFAULT CHARSET=utf8")  
	      db.query("insert into tb_test (id, text) values (1,'first line'),(2,'second line')")  
	      printf "%d rows were inserted\n",db.affected_rows  
	      rslt = db.query("select text from tb_test")  
	      while row = rslt.fetch_row do  
	        puts row[0]  
	      end  
	    rescue Mysql::Error => e  
	      puts "Error code: #{e.errno}"  
	      puts "Error message: #{e.error}"  
	      puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")  
	    ensure  
	      db.close if db  
	    end  

	end


end

jack = Person.new
jack.say()


然后再此文件的目录,输入:ruby ruby-mysql.rb  运行效果如下图:



你可能感兴趣的:(Ruby)