Sha256: a8a3adbc4fbfef65f706f46a471c2a2e46207edc8ebacd6ce3aea861cef2e811

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

# This script is an example of how to specify a default value for a column.
#
require 'kirbybase'

db = KirbyBase.new

# If table exists, delete it.
db.drop_table(:address_book) if db.table_exists?(:address_book)

# Create a table.  Notice how we specify a default value for :category.
address_book_tbl = db.create_table(:address_book,
 :firstname, :String, :lastname, :String, :street_address, :String,
 :city, :String, :phone, :String, 
 :category, {:DataType=>:String, :Default=>'Super Hero'})

# Insert a record.  Notice that I am passing nil for :category.  KirbyBase
# will insert the default value, 'Super Hero', in that field.
address_book_tbl.insert('Bruce', 'Wayne', '1234 Bat Cave', 'Gotham City',
 '111-111-1111', nil)

# Insert another record.  Here we supply the value for :category, so
# KirbyBase will use it instead of the default.
address_book_tbl.insert('Bugs', 'Bunny', '1234 Rabbit Hole', 'The Forest',
 '222-222-2222', 'Cartoon Character')

# Now lets change the default value for :category to 'President'.
address_book_tbl.change_column_default_value(:category, 'President')

# And let's add another record without supplying a value for :category.
address_book_tbl.insert(firstname='George', lastname='Bush',
 street_address='1600 Pennsylvania Ave', 'Washington', '333-333-3333', nil)

# Now, let's remove the default value for :category
address_book_tbl.change_column_default_value(:category, nil)

# And add another record.  We won't specify a value for :category and,
# KirbyBase will not use a default value, because we removed it.
address_book_tbl.insert('Silver', 'Surfer', '1234 Galaxy Way',
 'Any City', '444-444-4444', nil)

# Now lets print the table out and you will see how all of the defaults
# worked.
puts address_book_tbl.select.to_report

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
KirbyBase-2.5 examples/default_value_test/default_value_test.rb
KirbyBase-2.5.1 examples/default_value_test/default_value_test.rb