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

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    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

    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}"

    + SHCSHC, 3 weeks ago

    custom

    131 views, 0 favs, 0 embeds more stats

    * About this Tutorial
    * Hello World
    * Loop more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 131
      • 131 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 0
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories