Ruby with MySQL Establishing Connections  real_connect(host, user, passwd, db, table)  Return client version information  ...
query(q) do query. If query do not return result, return nil. affected_rows() return number of affected rows. num_rows() r...
fetch_row() return array of field value. fetch_hash() return hash of pair of field name and field value. if  with_table  I...
Example 1 require 'rubygems'  require 'mysql' mysql = Mysql.real_connect("localhost", "root", "&q...
Example 2 require 'rubygems'  require ‘mysql’ mysql = Mysql.real_connect("localhost", "root", "&q...
Example 3 require 'rubygems'  require ‘mysql’ mysql = Mysql.real_connect("localhost", "root", "&q...
Upcoming SlideShare
Loading in...5
×

About this Tutorial , Hello World , Loops , Branching , Containers , Arrays , Hashes , Strings ,Regular Expressions , Subroutines ,Exceptions ,Terminal IO ,File IO , How OOP is Ruby? , Object Oriented Progr

1,119

Published on

* About this Tutorial
* Hello World
* Loops
* Branching
* Containers
* Arrays
* Hashes
* Strings
* Regular Expressions
* Subroutines
* Exceptions
* Terminal IO
* File IO
* How OOP is Ruby?
* Object Oriented Programming Concepts
* Simple OOP in Ruby

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,119
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

About this Tutorial , Hello World , Loops , Branching , Containers , Arrays , Hashes , Strings ,Regular Expressions , Subroutines ,Exceptions ,Terminal IO ,File IO , How OOP is Ruby? , Object Oriented Progr

  1. 1. Ruby with MySQL Establishing Connections real_connect(host, user, passwd, db, table) Return client version information get_client_info() client_info() get_server_info() server_info() Return server version information.
  2. 2. query(q) do query. If query do not return result, return nil. affected_rows() return number of affected rows. num_rows() return number of rows. close() close connection.
  3. 3. fetch_row() return array of field value. fetch_hash() return hash of pair of field name and field value. if with_table Is true, hash key is table+&quot;.&quot;+fieldname. <ul><li>stat() </li></ul><ul><ul><li>return server status. </li></ul></ul><ul><li>list_tables() </li></ul><ul><ul><li>return array of table name. </li></ul></ul>
  4. 4. Example 1 require 'rubygems' require 'mysql' mysql = Mysql.real_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;, &quot;test&quot;) mysql.query(&quot;DROP TABLE IF EXISTS rocks&quot;) mysql.query(&quot;CREATE TABLE rocks (id INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), rockname CHAR(20) NOT NULL);&quot;) mysql.query(&quot;INSERT INTO rocks (rockname) values('Granite');&quot;) mysql.query(&quot;INSERT INTO rocks (rockname) values('Coal');&quot;) mysql.query(&quot;INSERT INTO rocks (rockname) values('Quartz');&quot;) mysql.close()
  5. 5. Example 2 require 'rubygems' require ‘mysql’ mysql = Mysql.real_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;, &quot;test&quot;) res = mysql.query(&quot;select * from rocks&quot;) puts mysql.stat() # return status of server puts mysql.list_tables # return array of table name. puts mysql.get_server_info() # return server info res.each do |row| col1 = row[0] col2 = row[1] puts col1 puts col2 end
  6. 6. Example 3 require 'rubygems' require ‘mysql’ mysql = Mysql.real_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;, &quot;test&quot;) res = mysql.query(&quot;SELECT * FROM rocks&quot;) while row = res.fetch_hash do printf &quot;%s, %s &quot;, row[&quot;id&quot;], row[&quot;rockname&quot;] end puts &quot;Number of rows returned: #{res.num_rows}&quot;
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×