Native ODBC access for python on linux?

Grig Gheorghiu grig.gheorghiu at gmail.com
Thu Jul 14 17:55:07 EDT 2005


1. Download and install MySQL-python from
<http://sourceforge.net/project/showfiles.php?group_id=22307&package_id=15775>

2. Try to connect to a MySQL database that you have running. In this
example I'm connecting to the bugs database installed with Bugzilla,
and I'm connecting as MySQL user root (not recommended for 'production'
code):

# python
Python 2.4 (#1, Nov 30 2004, 16:42:53)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> from pprint import pprint
>>> conn = MySQLdb.connect(db='bugs', user='root', passwd='***')
>>> cursor = conn.cursor()
>>> cursor.execute('select * from groups')
7L
>>> data = cursor.fetchall()
>>> print data
((1L, 'tweakparams', 'Can tweak operating parameters', 0, '', 1), (2L,
'editusers', 'Can edit or disable users', 0, '', 1), (4L,
'creategroups', 'Can create and destroy groups.', 0, '', 1), (8L,
'editcomponents', 'Can create, destroy, and edit components.', 0, '',
1), (16L, 'editkeywords', 'Can create, destroy, and edit keywords.', 0,
'', 1), (32L, 'editbugs', 'Can edit all aspects of any bug.', 0, '.*',
1), (64L, 'canconfirm', 'Can confirm a bug.', 0, '.*', 1))
>>> pprint(data)
((1L, 'tweakparams', 'Can tweak operating parameters', 0, '', 1),
 (2L, 'editusers', 'Can edit or disable users', 0, '', 1),
 (4L, 'creategroups', 'Can create and destroy groups.', 0, '', 1),
 (8L, 'editcomponents', 'Can create, destroy, and edit components.', 0,
'', 1),
 (16L, 'editkeywords', 'Can create, destroy, and edit keywords.', 0,
'', 1),
 (32L, 'editbugs', 'Can edit all aspects of any bug.', 0, '.*', 1),
 (64L, 'canconfirm', 'Can confirm a bug.', 0, '.*', 1))
>>>

For more info on how to use the MySQLdb module, see the docs at
<http://sourceforge.net/docman/?group_id=22307>

Hope this helps,

Grig




More information about the Python-list mailing list