Sha256: fcb86b1843f36d1c83346b60c9d60f37017addace64c36479a4ee411a36e18b3

Contents?: true

Size: 1.76 KB

Versions: 16

Compression:

Stored size: 1.76 KB

Contents

#!/usr/bin/env ruby

require 'rubygems'
require_gem 'fireruby'

include FireRuby

# SQL constants.
CREATE_TABLE_SQL = 'CREATE TABLE TESTTABLE (TESTID INTEGER NOT NULL PRIMARY '\
                   'KEY, TESTTEXT VARCHAR(100), TESTFLOAT NUMERIC(6,2), '\
                   'CREATED TIMESTAMP)'
DROP_TABLE_SQL   = 'DROP TABLE TESTTABLE'
INSERT_SQL       = 'INSERT INTO TESTTABLE VALUES(?, ?, ?, ?)'
SELECT_SQL       = 'SELECT * FROM TESTTABLE'

begin
   # Check if the database file exists.
   db = nil
   if File.exist?('./example.fdb') == false
      # Create the database file.
      db = Database.create('./example.fdb', 'sysdba', 'masterkey', 1024, 'ASCII')
   else
      # Create the databse object.
      db = Database.new('./example.fdb')
   end
   
   # Obtain a connection to the database.
   db.connect('sysdba', 'masterkey') do |cxn|   
      # Create the database table.
      cxn.execute_immediate(CREATE_TABLE_SQL)
      
      # Insert 50 rows into the database.
      decimal = 1.0
      cxn.start_transaction do |tx|
         s = Statement.new(cxn, tx, INSERT_SQL, 3)
         1.upto(20) do |number|
            s.execute_for([number, "Number is #{number}.", decimal, Time.new])
            decimal = decimal + 0.24
         end
         s.close
      end
      
      # Select back the rows inserted and display them
      rows = cxn.execute_immediate(SELECT_SQL)
      rows.each do |row|
         puts "-----"
         puts "Test Id:      #{row['TESTID']}"
         puts "Test Text:    '#{row['TESTTEXT']}'"
         puts "Test Float:   #{row['TESTFLOAT']}"
         puts "Test Created: #{row['CREATED']}"
         puts "-----"
      end
      rows.close
      
      # Drop the table.
      cxn.execute_immediate(DROP_TABLE_SQL)
   end
rescue Excepton => error
   puts error.message
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
fireruby-0.3.2-powerpc-darwin examples/example01.rb
fireruby-0.3.2-mswin32 examples/example01.rb
fireruby-0.3.2-i586-linux examples/example01.rb
fireruby-0.3.1-powerpc-darwin examples/example01.rb
fireruby-0.3.1-mswin32 examples/example01.rb
fireruby-0.3.1-i586-linux examples/example01.rb
fireruby-0.3.0-powerpc-darwin examples/example01.rb
fireruby-0.3.0-mswin32 examples/example01.rb
fireruby-0.3.0-i586-linux examples/example01.rb
fireruby-0.2.2-powerpc-darwin examples/example01.rb
fireruby-0.2.2-mswin32 examples/example01.rb
fireruby-0.2.2-i586-linux examples/example01.rb
fireruby-0.2.1-powerpc-darwin examples/example01.rb
fireruby-0.2.1-mswin32 examples/example01.rb
fireruby-0.2.0-powerpc-darwin examples/example01.rb
fireruby-0.2.0-mswin32 examples/example01.rb