Mysql – ruby examples.

After I got the mysql ruby module running, I tried out lots of small programs (some based off kitebirds tutorial) in order to try out different things.

First I ran simple.rb, which I used to test if the server is working. Works very well, easy to set up too, as the test database should already exist for you.

Simple.rb 

require “mysql”

   begin
       #this example is used for checking if your installation is correct.
       # connect to the MySQL server
       dbh = Mysql.real_connect(“localhost”, “testuser”, “testpass”, “test”)
       # get server version string and display it
       puts “Server version: ” + dbh.get_server_info
      
   rescue MysqlError => e
       print “Error code: “, e.errno, “\n”
       print “Error message: “, e.error, “\n”
   ensure
       # disconnect from server
       dbh.close
   end

I ran a few more based off kitebirds examples. Here is another sample program.

require “mysql”

   begin
       # connect to the MySQL server (kitebird test program)
       dbh = Mysql.real_connect(“localhost”, “testuser”, “testpass”, “test”)
       # get server version string and display it
       puts “Server version: ” + dbh.get_server_info
      
       #start of code example from kitebird
       #this example makes a table, addes data, check for data, then outputs it
      
       dbh.query(“DROP TABLE IF EXISTS password”)
       dbh.query(”
              CREATE TABLE password
               (
                user CHAR(45),
                upassword CHAR(45)
               )
         “)
       dbh.query(”
           INSERT INTO password (user, upassword)
           VALUES
               (‘snake’, ‘reptile’),
               (‘frog’, ‘amphibian’),
               (‘tuna’, ‘fish’),
               (‘racoon’, ‘mammal’)
       “)
        printf “%d rows were inserted\n”, dbh.affected_rows
   #end of code example from kitebird
  
   #start of code example from kitebird
      # issue a retrieval query, perform a fetch loop, print
      # the row count, and free the result set
      res = dbh.query(“SELECT user, upassword FROM password”)

     while row = res.fetch_row do
       printf “%s, %s\n”, row[0], row[1]
     end
     printf “%d rows were returned\n”, res.num_rows

     res.free
  
   #end of code example from kitebird

   rescue MysqlError => e
       print “Error code: “, e.errno, “\n”
       print “Error message: “, e.error, “\n”
   ensure
       # disconnect from server
       dbh.close
   end
 

Here is an example showing password encryption with sha1.

require “mysql”

   begin
       # connect to the MySQL server (kitebird test program)
       dbh = Mysql.real_connect(“localhost”, “testuser”, “testpass”, “test”)
       # get server version string and display it
       puts “Server version: ” + dbh.get_server_info
      
       #start of code example
         dbh.query(“DROP TABLE IF EXISTS password”)
        dbh.query(”
              CREATE TABLE password
               (
                user CHAR(45),
                upassword CHAR(45)
               )
         “)
        dbh.query(”
           INSERT INTO password (user, upassword)
           VALUES
               (‘raven’, SHA1(‘croak’)),
               (‘frog’, SHA1(‘ribbit’)),
               (‘lamb’, SHA1(‘maaa’)),
               (‘mouse’, SHA1(‘squeek’))
       “)
      
        printf “%d rows were inserted\n”, dbh.affected_rows
 
       # issue a retrieval query, perform a fetch loop, print
       # the row count, and free the result set
       res = dbh.query(“SELECT user, upassword FROM password”)

     while row = res.fetch_row do
       printf “%s, %s\n”, row[0], row[1]
     end
     printf “%d rows were returned\n”, res.num_rows
     printf “passwords are return encrypted\n”

     res.free
 
       #gets rid of table
 #dbh.query(“DROP TABLE password;”);

       #end of code example
  
   rescue MysqlError => e
       print “Error code: “, e.errno, “\n”
       print “Error message: “, e.error, “\n”
   ensure
       # disconnect from server
       dbh.close
   end

Ill try to post some more examples you can run, based off simple.rb as I get chance.
http://www.kitebird.com/articles/ruby-mysql.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: