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
×
 

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

on

  • 1,860 views

* 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

Statistics

Views

Total Views
1,860
Views on SlideShare
1,858
Embed Views
2

Actions

Likes
0
Downloads
9
Comments
0

1 Embed 2

http://www.slideshare.net 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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 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 Presentation Transcript

  • 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.
  • 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.
  • 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.
  • 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()
  • 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
  • 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}"