In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "the installation and configuration of mysql under ruby". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the installation and configuration of mysql under ruby".
Ruby on rails installs mysql database
1. Install mysql in the system: download mysql software, which can be downloaded from many download addresses on Baidu, or go to the official Chinese website of mysql to download and install the software. I chose version 5.1.62.
Install mysql and set the default character set to utf-8
Copy the libmySQL.dll under the bin directory of the mysql installation directory (default C:/program files/mysql) to the C:/ruby/bin directory
2. Install mysql-related drivers:
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 the / lib/ruby/site_ruby/1.8/i386-msvcrt/ directory under the ruby installation directory.
4. Test ruby connection to mysql
Enter the code after creating a new text test_mysql.rd:
Require "mysql"
Puts ("hello,mysql")
Command line executes ruby test_mysql.rd
Output result:
Hello,mysql
Then the mysql connection is successful
Write a piece of code:
Require 'DBcon'
Begin
Dbh = Mysql.real_connect ("localhost", "root", "sa", "makedish", 3306)-# the real_connect method of the Mysql module is called here. Connect to the database this machine: 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")
Dbh.query ("insert into test_foolfish values (2) hello")
Printf "% d rows were insertedn", dbh.affected_rows-# affected_rows returns the number of rows affected
Res=dbh.query ("SELECT name FROM test_foolfish")
Puts "= n"
Res.each_hash (with_table = true) do | row |
Printf "% djm% 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
In ruby, there are two operations on the database: one is not to return the result set (such as insert,update,delete, etc.), and the other is to return the result set (such as select show, etc.). For operations that do not return a result set, we just need to use the dbh.query method and pass in the sql statement that needs to be executed.
It is relatively troublesome for the other type that needs to return the result set. After executing a similar statement above, we need to process the result set. We can present the result set as an array or in the form of hash. Here we use hash to show.
Res=dbh.query ("SELECT name FROM test_foolfish")
Puts "=\ n"
Res.each_hash (with_table = true) do | row |
Printf "% djin% s\ n", row ["test_rb.id"], row ["test_rb.name"]
End
Adding the with_table = true parameter to the each_hash method ensures the embarrassment of multiple tables with the same field when executing a multi-table query. In this way, the key value of our hash can appear in the form of "table name. column name".
At this point, I believe you have a deeper understanding of "the installation and configuration of mysql under ruby". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.