Installation guide
16. Examples
16. Examples
Here is a very simple example of how to use mxODBC. More elaborate examples
of using Python Database API compatible database interfaces can be found in the
Database Topic Guide on http://www.python.org/
. Andrew Kuchling's introduction
to the Python Database API is an especially good reading. There are also a few
books on using Python DB API compatible interfaces, some of them cover
mxODBC explicitly.
On Unix:
>>> import mx.ODBC.iODBC
>>> db = mx.ODBC.iODBC.DriverConnect('DSN=database;UID=user;PWD=passwd')
>>> c = db.cursor()
>>> c.execute('select count(*) from test')
>>> c.fetchone()
(305,)
>>> c.tables(None,None,None,None)
8
>>> mx.ODBC.print_resultset(c)
Column 1 | Column 2 | Column 3 | Column 4 | Column 5
---------------------------------------------------------------
'' | '' | 'test' | 'TABLE' | 'MySQL table'
'' | '' | 'test1' | 'TABLE' | 'MySQL table'
'' | '' | 'test4' | 'TABLE' | 'MySQL table'
'' | '' | 'testblobs' | 'TABLE' | 'MySQL table'
'' | '' | 'testblobs2' | 'TABLE' | 'MySQL table'
'' | '' | 'testdate' | 'TABLE' | 'MySQL table'
'' | '' | 'testdates' | 'TABLE' | 'MySQL table'
'' | '' | 'testdatetime' | 'TABLE' | 'MySQL table'
>>> c.close()
>>> db.close()
>>>
On Windows:
>>> import mx.ODBC.Windows
>>> db =
mx.ODBC.Windows.DriverConnect('DSN=database;UID=user;PWD=passwd')
>>> c = db.cursor()
>>> c.execute('select count(*) from test')
>>> c.fetchone()
(305,)
>>> c.tables(None,None,None,None)
8
>>> mx.ODBC.print_resultset(c)
Column 1 | Column 2 | Column 3 | Column 4 | Column 5
---------------------------------------------------------------
'' | '' | 'test' | 'TABLE' | 'MySQL table'
'' | '' | 'test1' | 'TABLE' | 'MySQL table'
'' | '' | 'test4' | 'TABLE' | 'MySQL table'
'' | '' | 'testblobs' | 'TABLE' | 'MySQL table'
'' | '' | 'testblobs2' | 'TABLE' | 'MySQL table'
'' | '' | 'testdate' | 'TABLE' | 'MySQL table'
'' | '' | 'testdates' | 'TABLE' | 'MySQL table'
'' | '' | 'testdatetime' | 'TABLE' | 'MySQL table'
>>> c.close()
>>> db.close()
>>>
As you can see, mxODBC has the same interface on Unix and Windows which
makes it an ideal basis for writing cross-platform database applications.
185