Install and configure MySQL in ruby

Source: Internet
Author: User
Rubyonrails installs mysql database and mysql in the system: Download mysql software. Many of them can be downloaded on Baidu or go to the official mysql website.

Ruby on rails install mysql database, install mysql in the system: Download mysql software, many can be downloaded on Baidu, or go to the mysql Chinese official website

Installing mysql databases using ruby on rails

1. Install mysql in the system: Download mysql software. Many of them can be downloaded on Baidu, or go to the mysql Chinese official website to download the installation software. I chose version 5.1.62.

Install mysql and set the default character set to UTF-8

Copy libmySQL. dll in the bin directory of the mysql installation directory (C:/program files/mysql by default) to the C:/ruby/bin directory.

2. Install the mysql DRIVER:

Gem install mysql-2.8.1-x86-mingw32.gem

3. Copy the mysql_api file in the lib/ruby/gems/1.8/gems/mysql-2.7.1-mswin32/ext/directory of the ruby installation directory to/lib/ruby/site_ruby/ 1.8/i386-msvcrt/directory.

4. Test ruby connection to mysql

Enter the code after creating the text test_mysql.rd:

Require "mysql"

Puts ("hello, mysql ")

Run ruby test_mysql.rd on the command line.

Output result:

Hello, mysql

Mysql connection successful

Write a piece of code:

Require 'dbcon'
Begin
Dbh = Mysql. real_connect ("localhost", "root", "sa", "makedish", 3306) ------ # Call the real_connect method of the Mysql module. Connect to the local database: User name: root Password: sa database: makedish port: 3306
Dbh. query ("drop table if exists test_foolfish") --- # ruby execution statement
Dbh. query ("create table test_foolfish (id int, name varchar (20 ))")
Dbh. query ("insert into test_foolfish values (1, '')")
Dbh. query ("insert into test_foolfish values (2, hello )")
Printf "% d rows were insertedn", dbh. affected_rows ---- # affected_rows returns the affected number of rows

Res = dbh. query ("SELECT name FROM test_foolfish ")
Puts "======================= n"
Res. each_hash (with_table = true) do | row |
Printf "% d, % sn", row ["test_rb.id"], row ["test_rb.name"]
End
Puts "======================= n"
Puts "Server version:" + dbh. get_server_info
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
Dbh. close if dbh
End

There are two types of operations on the database in ruby: one is that the result set is not returned (such as insert, update, delete, and so on) one is to return the result set (such as select show ). For operations that do not return a result set, we only need to use the dbh. query method and pass in the SQL statement to be executed.

It is more difficult for another type to return a result set. After the preceding statements are executed, we need to process the result set. We can present a result set in an array or hash format. Here we use the hash mode to display.

Res = dbh. query ("SELECT name FROM test_foolfish ")

Puts "=========================\ n"

Res. each_hash (with_table = true) do | row |

Printf "% d, % s \ n", row ["test_rb.id"], row ["test_rb.name"]

End

The parameter with_table = true added to the each_hash method ensures that multiple tables have the same fields when performing multi-table queries. In this way, the hash key value can appear in the form of "table name. Column name.

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.