Your SlideShare is downloading. ×
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
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

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,100
views

Published on

* About this Tutorial …

* 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,100
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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. 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. 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+"."+fieldname.
    • stat()
      • return server status.
    • list_tables()
      • return array of table name.
  • 4. Example 1 require 'rubygems' require 'mysql' mysql = Mysql.real_connect("localhost", "root", "", "test") mysql.query("DROP TABLE IF EXISTS rocks") mysql.query("CREATE TABLE rocks (id INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), rockname CHAR(20) NOT NULL);") mysql.query("INSERT INTO rocks (rockname) values('Granite');") mysql.query("INSERT INTO rocks (rockname) values('Coal');") mysql.query("INSERT INTO rocks (rockname) values('Quartz');") mysql.close()
  • 5. Example 2 require 'rubygems' require ‘mysql’ mysql = Mysql.real_connect("localhost", "root", "", "test") res = mysql.query("select * from rocks") 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. Example 3 require 'rubygems' require ‘mysql’ mysql = Mysql.real_connect("localhost", "root", "", "test") res = mysql.query("SELECT * FROM rocks") while row = res.fetch_hash do printf "%s, %s ", row["id"], row["rockname"] end puts "Number of rows returned: #{res.num_rows}"