Question about ODBC

Karsten Weinert k.weinert at gmx.net
Tue Aug 27 22:22:07 EDT 2002


Hi,
I can not understand the following behavior of the Access ODBC driver 
and/or Python. The problem is: I open a ODBC connection, do some
SQL-statements, for example create a new table an put some data in it.
Then I close the connection. When I open the connection again, the new
table has disappeared!

Here is some code which illustrates this:
#---------------------- snip
import mx.ODBC.Windows

dsn="DataSourceName"
conn=None
curs=None

def test():
  # open connection
  conn=mx.ODBC.Windows.Connect(dsn)
  curs=conn.cursor()

  # create a table
  sqlmsg='CREATE TABLE test13 (nr Integer, val Integer)'
  print(sqlmsg)
  curs.execute(sqlmsg)

  # put some data in the table
  for i in range(10):
    sqlmsg="INSERT INTO test13 VALUES (%d,%d)" % (i, i*i)
    print(sqlmsg)
    curs.execute(sqlmsg)

  # see that there is really data in the table
  curs.execute("SELECT count(nr) FROM test13")
  res=curs.fetchall()
  print("There are now %s rows in the table." % res[0][0])

  # but what happens when the connection is closed and reopened?
  curs.close()
  conn.close()
  conn=mx.ODBC.Windows.Connect(dsn)
  curs=conn.cursor()
  curs.execute("SELECT count(nr) FROM test13")
  res=curs.fetchall()
  print("There are now %s rows in the table." % res[0][0])

  
I am using python 2.2 on Win98 with the Access-ODBC-driver (Desktop
Driver Pack 3.0). I don't have Access itself installed.

Can you please give me some advice how to avoid this? 

Regards,
Karsten



More information about the Python-list mailing list