Ruby practice-connect to MySQL

Source: Internet
Author: User

Installation environment:

OS: Windows XP

RUBY: ruby1.9.1

MySQL: mysql5.1.46 (username/password: Root/root port: 3306)

Ruby-MySQL DRIVER: mysql-2.8.1-x86-mswin32.gem

(Note: drivers of version 2.7.3 will appear during testing.Require "MySQL", the specified module cannot be foundError)

IDE: rubymine2.0.1

 

The installation of Ruby, rubymine, and MySQL is not mentioned here. Please refer to the steps for installing the driver.

1) in the bin directory of the MySQL installation directory, findIbmysql. dllCopy the file to the bin directory of the Ruby installation directory.

2) http://rubyforge.org/frs? Group_id = 1598, download the mysql-2.8.1-x86-mswin32.gem, which is the mysql_ruby driver.

Command Line, enter the directory where the file is located, run the gem install mysql-2.8.1-x86-mswin32.gem, the installation is successful.

 

Create a ruby test class

Mysqltest. Rb

Class mysqltest <br/> # code here <br/> require "MySQL" <br/> def testmysql <br/> DBC = MySQL. real_connect ('localhost', 'root', 'root', 'mysql') <br/> res = DBC. query ('select * From user') <br/> puts "test MySQL .... "<br/> while ROW = res. fetch_row DO <br/> puts "# {row [0]}" <br/> end <br/> def createtable <br/> DBC = MySQL. real_connect ('localhost', 'root', 'root', 'test') <br/> DBC. query ("Drop table if Exists cux_users ") <br/> DBC. query ("create table cux_users (ID int, name char (20)") <br/> DBC. query ("insert into cux_users values (1, 'Tom ')") <br/> printf "create table ........ "<br/> printf" % d rows were inserted/N ", DBC. affected_rows <br/> res = DBC. query ("Select name from cux_users") <br/> puts "= select data =/N" <br/> while ROW = res. fetch_row DO <br/> printf "% s, % s/n", row [0], row [1] <br/> E Nd <br/> puts "===========================/ N" <br/> puts "server version: "+ DBC. get_server_info <br/> rescue MYSQL: Error => E <br/> puts "error code: # {e. errno} "<br/> puts" error message: # {e. error} "<br/> puts" error sqlstate: # {e. sqlstate} "If e. respond_to? ("Sqlstate") <br/> ensure <br/> puts "close connection ....... "<br/> DBC. close if DBC <br/> end <br/> (mysqltest. new ). testmysql <br/> (mysqltest. new ). createtable <br/> end

 

Test results:

C:/ruby19/bin/ruby.exe-e stdout. sync = true; stderr. sync = true; load ($0 = argv. shift) E:/Ruby/test/COM. crystal/mysql_test.rb
Test MySQL ....
Localhost
Create Table...
=== Select data ===
Tom,
============================
Server version: 5.1.46-community
Close connection .......

Process finished with exit code 0

 

View database Test

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.