Install and configure mysql in ruby

Source: Internet
Author: User

Install and configure mysql under ruby on rails install mysql database 1. Install mysql in the system: Download mysql software, many of which can be downloaded on Baidu, or go to the mysql Chinese official website to download and install the software. I chose 5.1.62 to install mysql. Set the default character set to UTF-8 and set the mysql installation directory (default C:/program files/mysql) libmySQL in the bin directory. copy the dll 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 under the lib/ruby/gems/1.8/gems/mysql-2.7.1-mswin32/ext/directory of the ruby installation directory to/lib/ruby under the ruby installation directory /site_ruby/1.8/i386-msvcrt/directory. 4. Test the ruby connection to mysql. Create the text test_mysql.rd and enter the code: require "mysql" puts ("hello, mysql") to run the ruby test_mysql.rd command. The output result is hello, for mysql, a piece of code is successfully written for mysql connection: 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_foolfis H ") 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_inforescue 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 dbhend ruby has two types of operations on the database: 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 result sets, you 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"], the with_table = true parameter is added to the row ["test_rb.name"] end each_hash method to ensure 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.

Related Article

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.