Executing SQL Statements and Navigating in the Result Set

In this example you use the methods of the Class SAPDB_ResultSet of the sdb.sql module. You create a table, fill it with values, execute an SQL statement and position the cursor at different places in the result set.

Procedure

...

       1.      Create a new table customer1 and fill it with data: 

session.sql ('CREATE TABLE customer1 (cno FIXED(4) PRIMARY KEY)')

insert = session.prepare ('INSERT INTO customer1 (cno) VALUES (?)')

for i in xrange (1, 11):

    insert.execute ([i])

       2.      Execute the following SQL statement:

cursor = session.sql ('SELECT cno FROM customer1 ORDER BY cno')

The SQL statement returns a result set. The cursor is positioned before the first data record of the result set.

       3.      Position the cursor at various places in the result set and display the respective data record:

                            a.      Next data record (first data record in the result set):

print cursor.next ()

(1,)

                            b.      Next data record:

print cursor.next ()

(2,)

                            c.      Previous data record:

print cursor.previous ()

(1,)

                            d.      Five data records forwards:

print cursor.relative (5)

(6,)

                            e.      One data record back:

print cursor.relative (-1)

(5,)

                              f.      Third data record:

print cursor.absolute (3)

(3,)

                            g.      Third-to-last data record:

print cursor.absolute (-3)

(8,)

                            h.      First data record:

print cursor.first ()

(1,)

                              i.      Last data record:

print cursor.last ()

(10,)

                              j.      Current data record:

print cursor.current ()

(10,)

       4.      Iterate through the entire result set and display all data records:

for row in cursor: print row

(1,)

(2,)

(3,)

(4,)

(5,)

(6,)

(7,)

(8,)

(9,)

(10,)

See also:

Examples for the Module sdb.sql