[DB-SIG] SQLserver and mySQL examples for a newbie

M.-A. Lemburg mal@lemburg.com
Sat, 27 Oct 2001 17:07:00 +0200


ronny.de_winter@alcatel.be wrote:
> 
> Can somebody give me some small examples on how to connect to a
> MS SQLServer / mySQL database and run a Query on it.

Here are some hints for Windows [and a shameless plug for mxODBC ;-)]:

Provided you have installed the ODBC drivers for MS SQL and
MySQL, just get mxODBC, install it and then try:

from mx.ODBC import Windows as DBI
db = DBI.DriverConnect('DSN=datasourcename;UID=username;PWD=password')
cursor = db.cursor()
cursor.execute('select * from mytable')
aList = cursor.fetchall()

BTW, the Win32 book on Python by Mark Hammond and Andy Dustman
has a very good introduction on how to connect to databases.

On Unix things work more or less the same, but you have to install
the ODBC manager first and then setup the datasources according
to the ODBC manager docs. The most popular ODBC managers for Unix
are www.iODBC.org and www.unixODBC.org.

mxODBC works with both of them in pretty much the same way
as on Windows except that the first line becomes:

from mx.ODBC import iODBC as DBI
-or-
from mx.ODBC import unixODBC as DBI

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Consulting & Company:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/