473,473 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Database interfacing

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

Jul 18 '05 #1
3 1386
"Michael T. Babcock" <mb******@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.

--
ha************@boeing.com
6-6M31 Knowledge Management
Phone: (425) 342-5601
Jul 18 '05 #2
On Fri, 09 Jan 2004 10:45:06 -0500, Michael T. Babcock wrote:
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).


You might find dbrow helpful:
http://opensource.theopalgroup.com/
http://opensource.theopalgroup.com/files/db_row-0.8.tgz
http://opensource.theopalgroup.com/files/db_row.py

--
George Young
gee arr wh*@ll.mit.edu
[three letter username]

Jul 18 '05 #3
Harry George wrote:
"Michael T. Babcock" <mb******@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)

userSchema = schema.lookupName( 'users' )
data = userSchema.itemClass(
FirstName = 'Michael',
LastName= 'Babcock'
Age= 99,
)
data.insertQuery( APPLICATION.getDBConnection() )
... 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)")

users = userSchema.query( """SELECT * FROM Users WHERE Age >= 99)""",
APPLICATION.getDBConnection())
... which would return me an array of dictionary items like:

[ {'ID': 143, 'FirstName': 'Michael' ... }, {'ID': 242, ... }, ... ]
In this case, a row of wrappers around dictionaries (objects). You can
change what class those objects are by declaring it in the userSchema
object. The default ones are fairly heavy wrappers with properties for
each field in the table, coercian of field-values to specified
classes/types, insert/refresh/update/delete query methods, etceteras.
You can write tools like this yourself or use existing tools. For
starters take a look at: http://skunkweb.sourceforge.net/PyDO/

Or, for the code above, PyTable RDBMS Manager.
http://pytable.sourceforge.net/ I mostly use it with PostgreSQL, but it
does have MySQL-compatible plumbing as well.
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.)

PyTable normally goes Python-definition -> database, but does have some
support for reading the definition out of the database.

There are, as Harry points out, a number of such wrapper mechanisms
available. The biggest problem with all of them is that it's almost as
much effort to learn and adapt them as it is to write your own. The
impedence mismatch between OO programming (Python) and RDBMS just
doesn't seem to allow for a "perfect" solution to the problem, so
instead you see a number of different approaches with different
strengths and weaknesses.

Good luck,
Mike

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/


Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shaurya Anand | last post by:
I would like to know more about Interfacing hardware devices through .NET. We're students and what we're trying to do is control our robots with printer or serial port interfacing... very similar...
3
by: Domagoj Cajic | last post by:
First i want to say that i just subscribed to this mailing list and this is my first post. :) im new to postgresql and i installed it recently on linux redhat 9 system following the instructions...
3
by: Cherry | last post by:
I have an ID Innovation card reader that I am trying to connect to my Access database. The first problem is that when I swipe the card through the reader, it reads the id but it adds digits to the...
1
by: 4 | last post by:
I am having some trouble w/ a school project database that I am doing. The database is used for a ski rental shop. In the rental table I have various customers w/ rentals on different days. I have...
5
by: Claudio Grondi | last post by:
I have just started to play around with the bsddb3 module interfacing the Berkeley Database. Beside the intended database file databaseFile.bdb I see in same directory also the __db.001...
1
by: Neil | last post by:
Our hosting company's server went down a few days ago. After they fixed it, our PHP Page and Database are not interfacing. They are not believing me that I have done nothing on my end in several...
0
by: garyusenet | last post by:
At least I think it's using an API (i don't totally understand the concept.) I'm using Act 6. I need to interface with the act database and add 'contacts' to the database. The reason I need to...
5
by: garyusenet | last post by:
At least I think it's using an API (i don't totally understand the concept.) I'm using Act 6. I need to interface with the act database and add 'contacts' to the database. The reason I need to...
1
by: TerryStone | last post by:
I am writing a Windows application, with an SQL Server (Express) database. The application is single user. The Windows application is the only application that accesses the database. I have...
17
by: darien.watkins | last post by:
Kindof a poll, kindof curiosity... What is your favorite python - database combination? I'm looking to make an app that has a local DB and a server side DB. I'm looking at python and sqlite...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.