What is a good pywin32 odbc connector documentation and tutorial on the web?
Alternatives:
- mxODBC by egenix.com (if you need ODBC)
- pyODBC
- sqlalchemy and DB-API 2.0 modules (which isn't ODBC) but it's maybe better alternative
The answer is: 'there isn't one'. However, here is an example that shows how to open a connection and issue a query, and how to get column metadata from the result set. The DB API 2.0 specification can be found in PEP 249.
import dbi, odbc
SQL2005_CS=TEMPLATE="""\
Driver={SQL Native Client};
Server=%(sql_server)s;
Database=%(sql_db)s;
Trusted_Connection=yes;
"""
CONN_PARAMS = {'sql_server': 'foo',
'sql_db': 'bar'}
query = "select foo from bar"
db = odbc.odbc(SQL2005_CS_TEMPLATE % CONN_PARAMS)
c = db.cursor()
c.execute (query)
rs = c.fetchall() # see also fetchone() and fetchmany()
# looping over the results
for r in rs:
print r
#print the name of column 0 of the result set
print c.description[0][0]
#print the type, length, precision etc of column 1.
print c.description[1][1:5]
db.close()
The only 'documentation' that I found was a unit test that was installed with the pywin32 package. It seems to give an overview of the general functionality. I found it here:
python dir\Lib\site-packages\win32\test\test_odbc.py
I should also point out that I believe it is implements the Python Database API Specification v1.0, which is documented here:
http://www.python.org/dev/peps/pep-0248/
Note that there is also V2.0 of this specification (see PEP-2049)
On a side note, I've been trying to use pywin32 odbc, but I've had problems with intermittent crashing with the ODBC driver I'm using. I've recently moved to pyodbc and my issues were resolved.