Accessing and Updating Data Using Python

After a connection is established, use a cursor object to manage the context of a fetch operation.

A cursor object provides access to methods to prepare and execute queries, and fetch rows from a result set. The cursor object is obtained from the connection object using the cursor method. This example shows how an application accesses and updates data:

import sybpydb

   #Create a connection.
   conn = sybpydb.connect(user='sa')

   # Create a cursor object.
   cur = conn.cursor()

   cur.execute("drop table footab")
   cur.execute("create table footab ( id integer, first char(20) null, last char(50) null)")
   cur.execute("insert into footab values( ?, ?, ? )", (1, "John", "Doe"))
   cur.execute("select * from footab")
   rows = cur.fetchall()
   for row in rows:
       print "-" * 55
       for col in range (len(row)):
          print "%s" % (row[col]),

   #Close the cursor object
   cur.close()

   #Close the connection
   conn.close()