Locked History Attachments

RdbExample

Oracle/Rdb examples

The first example shows how to perform a read only transaction on a database.

Source code:

   1 import string
   2 import pprint
   3 import sys, traceback
   4 import rdb
   5  
   6 curs = rdb.statement("""select rdb$relation_name from rdb$relations 
   7 where rdb$system_flag = ? order by rdb$relation_name""")
   8  
   9 rdb.attachDB('mf_personnel')
  10  
  11 print
  12 print 'User Tables'
  13 rdb.read_only()
  14 curs.execute(0)
  15 r = curs.fetchone()
  16 i = 0
  17 while (r and i < 4):
  18     print "user relation name = ", r[0]
  19     r = curs.fetchone()
  20     i += 1
  21 curs.close()
  22 rdb.commit()
  23  
  24 print
  25 print 'System Tables'
  26 rdb.read_only()
  27 curs.execute(1)
  28 r = curs.fetchone()
  29 i = 0
  30 while (r and i < 4):
  31     print "system relation name = ", r[0]
  32     r = curs.fetchone()
  33     i += 1
  34 curs.close()
  35 rdb.commit()
  36  
  37 # use an iterator
  38 print
  39 print 'User Tables using an iterator'
  40 rdb.read_only()
  41 curs.execute(0)
  42 i = 0
  43 for line in curs:
  44     i += 1
  45     if i > 4: break
  46     print "user relation name = ", line[0]
  47 rdb.commit()
  48  
  49 # use fetchall
  50 # fetchall automatically close the cursor
  51 print
  52 print 'User Tables using fetchall'
  53 rdb.read_only()
  54 curs.execute(0)
  55 r = curs.fetchall()
  56 rdb.commit()
  57 pprint.pprint(r)

Result:

User Tables
user relation name =  CANDIDATES
user relation name =  COLLEGES
user relation name =  CURRENT_INFO
user relation name =  CURRENT_JOB

System Tables
system relation name =  RDB$COLLATIONS
system relation name =  RDB$CONSTRAINTS
system relation name =  RDB$CONSTRAINT_RELATIONS
system relation name =  RDB$DATABASE

User Tables using an iterator
user relation name =  CANDIDATES
user relation name =  COLLEGES
user relation name =  CURRENT_INFO
user relation name =  CURRENT_JOB

User Tables using fetchall
(('CANDIDATES                     ',),
 ('COLLEGES                       ',),
 ('CURRENT_INFO                   ',),
 ('CURRENT_JOB                    ',),
 ('CURRENT_SALARY                 ',),
 ('DEGREES                        ',),
 ('DEPARTMENTS                    ',),
 ('EMPLOYEES                      ',),
 ('JOBS                           ',),
 ('JOB_HISTORY                    ',),
 ('RESUMES                        ',),
 ('SALARY_HISTORY                 ',),
 ('WORK_STATUS                    ',))

The second example is a DCL procedure that creates a database so the usage of inserts and updates can be demonstrated.

Source code:

$!
$! Create a test database
$!
$     sql
$     deck
create database filename test;
create table test_tbl (
    test_number integer primary key not deferrable,
    test_name varchar(20)
);
commit;
start transaction read write;
insert into test_tbl (test_number, test_name) values (1, 'one');
insert into test_tbl (test_number, test_name) values (2, 'two');
insert into test_tbl (test_number, test_name) values (3, 'three');
commit;
$     eod
$!
$! Create a python script
$!
$ create test.py
$ deck

   1 import rdb
   2 import sys
   3 
   4 rdb.attachDB ('test')
   5 
   6 rdb.read_only ()
   7 
   8 # Note the question mark in the statement.  An integer will be substituted for
   9 # it in the execute method...
  10 
  11 sql = """select * from test_tbl where test_number = ?"""
  12 curs = rdb.statement (sql)
  13 
  14 # The integer 2 is being substituted for the question mark and the sql
  15 # statement executes, populating the cursor.  Then the fetchone method gets
  16 # the data into our python variable.
  17 
  18 curs.execute (2)
  19 data = curs.fetchone ()
  20 
  21 # Select statements create a cursor in the database.  Don't forget to close
  22 # it.
  23 
  24 curs.close ()
  25 
  26 # The results end up in a tuple with the data in the same order as the
  27 # column definitions in the table.
  28 
  29 print data
  30 
  31 rdb.rollback ()
  32 
  33 # Now an insert.  Again, note the question marks.  Pay particular attention to
  34 # the fact that even though one variable is an integer and the other is a
  35 # string, you don't need to put single quotes around the string.  the rdb
  36 # module takes care of that for you.  The same goes for dates.  Just a simple
  37 # question mark is enough.  No need to specify 'DATE ANSI' in front of the
  38 # substitution marker.
  39 
  40 rdb.read_write ()
  41 
  42 sql = """insert into test_tbl (test_number, test_name) values (?, ?)"""
  43 
  44 ins_curs = rdb.statement (sql)
  45 ins_curs.execute (4, 'for')
  46 
  47 # Oops, that should have been 'four'.  Let's do an update...
  48 
  49 sql = """update test_tbl set test_name = ? where test_number = ?"""
  50 
  51 upd_curs = rdb.statement (sql)
  52 upd_curs.execute ('four', 4)
  53 
  54 rdb.commit ()
  55 
  56 # Now let's demonstrate some error handling.
  57 
  58 rdb.read_write ()
  59 
  60 try:
  61     ins_curs.execute (1, 'one')
  62 except rdb.Error, err:
  63     if err[0] == -1001:         # Constraint violation
  64         print 'Duplicate value ignored.'
  65         pass
  66     else:
  67         print 'Caught Rdb error!'
  68         print err
  69         sys.exit ()
  70 except rdb.Warning, warn:
  71     print 'Caught Rdb warning!'
  72     print warn
  73     sys.exit ()
  74 
  75 rdb.rollback ()
  76 
  77 # Now show all the data in the table using fetchall.
  78 
  79 rdb.read_only ()
  80 
  81 sql = """select * from test_tbl"""
  82 sel_curs = rdb.statement (sql)
  83 sel_curs.execute ()
  84 
  85 data = sel_curs.fetchall ()
  86 
  87 # Note there is no cursor close here.  The fetchall method does it
  88 # automatically.
  89 
  90 for d in data:
  91     print d
  92 
  93 rdb.rollback ()

$ eod
$ python test.py
$
$!
$! Clean up
$!
$ delete/nolog test.rdb;
$ delete/nolog test.snp;
$ exit

Result:

1 row inserted
1 row inserted
1 row inserted
(2, 'two')
Duplicate value ignored.
(1, 'one')
(2, 'two')
(3, 'three')
(4, 'four')