Database interfacing

Harry George harry.g.george at boeing.com
Fri Jan 9 11:57:00 CET 2004


"Michael T. Babcock" <mbabcock at fibrespeed.net> writes:

> I'm working with databases (MySQL primarily) more and more with
> Python, and having used PERL in the past for similar work, I'm
> wondering if there are good tools for doing 'intelligent'
> selects/inserts to/from dictionaries, etc.  For example:
> 
> data = {'FirstName': 'Michael', 'LastName': 'Babcock', 'Age': 99}
> lastid = db.insert('Users', data)
> 
> ... which would execute "INSERT INTO Users (FirstName, LastName, Age)
> VALUES ('Michael', 'Babcock', 99)"
> 
> And also helpful would be:
> 
> data = db.query("dbname", "SELECT * FROM Users WHERE Age >= 99)")
> 
> ... which would return me an array of dictionary items like:
> 
> [ {'ID': 143, 'FirstName': 'Michael' ... }, {'ID': 242, ... }, ... ]
> 
> Just curious, thanks (I've written a piece of code to generate the
> array of dictionary results myself actually, but I'm sure someone
> else's is better).
> 
> -- 
> Michael T. Babcock
> C.T.O., FibreSpeed Ltd.
> http://www.fibrespeed.net/~mbabcock
> 
> 
> 

You can write tools like this yourself or use existing tools.  For
starters take a look at: http://skunkweb.sourceforge.net/PyDO/

If you roll your own, here are some considerations:

You need to synchronize the table definitions in the DBMS with the
class definitions in python.  Some approaches start with the DBMS and
extract the table definitions (attributes, keys, etc) for use in
generating the needed python code.  Some start with python and define
a list of attributes and their meta data as a class-level var for a
class.  (I've tried both ways.)

Python-first opens the door to aspectual programming.  But you may
need both approaches when you need to work with existing code or
databases.

-- 
harry.g.george at boeing.com
6-6M31 Knowledge Management
Phone: (425) 342-5601



More information about the Python-list mailing list