473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Better access to database search results

Usually when I access db search results it's something like this:

cursor.execute("select A1,A2,A3,A4 from B where C")
for (a1,a2,a3,a4) in cursor.fetchall():
stuff()

But sometimes the point at which I use the data returned is not with the
search, and so having the ability to access the results as a dictionary
is more useful. I know that with MySQLdb I can open the connection and
have it return a dictionary, but when only one search out of all the
requests needs to be that way, it seems too much hassle to get a new
cursor just for that. Also, as a web developer that uses python, my
designers cognate "name.attribute" much easier than "name['attribute']"
and so I came up with this function to help us both out.

Let me know what you think. Could this be done simpler?

def convert_cursor(cursor):
'''Converts a cursor object with a result set in tuples to a list of
dicts, where the members of the keys are the field names, and the
values are
the field values.'''
fields = map(lambda x: x[0], cursor.description)
class DictObj(dict):
'''
DictObj is a normal dictionary that allows you to access its members
via ``var[key]`` as well as ``var.key``.
'''
def __init__(self,dic):
dict.__init__(self,dic)
self.__dict__ = self
return [DictObj(zip(fields,x)) for x in cursor.fetchall()]

Jul 18 '05 #1
0 1041

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

Similar topics

22
by: David Sterling | last post by:
After much futzing about with the XML/XSD for the Search.Response, I had to resort to this... PLEASE Tell me there is a better way! private void ExecuteAQuery(string strLookingForWhat) {...
1
by: Jack-of-all-traits | last post by:
This is a big problem and I really need help, no one seems to know how to solve this problem. I want to take the data from a record in a particular a field that contains the names of generic...
0
by: Mishu | last post by:
I have a document log as Access 2000 database. Each record has a corresponding pdf and txt file stored in the same folder as the database. There is a hyperlink field that points to the...
8
by: jquest | last post by:
Hi Again; I have had help from this group before and want to thank everyone, especially PCDatasheet. My database includes a field called HomePhone, it uses the (xxx)xxx-xxx format to include...
26
by: Stav | last post by:
Hi there. I'm working on an application that currently uses DAO to connect to an Access 97 database. The database is created by and used exclusively by the product to store search results and...
2
by: Homey! | last post by:
Hello all I am new to Access. I have imported data from an old FoxPro 2.x database. This is probably the most basic function but I cant get a search box to work. I need to search for company name...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
1
by: Webstorm | last post by:
Hi, I hope someone can help me sort this out a bit, Im completely lost. Here is the page I am working on: http://www.knzbusinessbrokers.com/default.asp I have 3 search critera that I need to...
1
by: Brit | last post by:
I have an ASP file that retrieves names from an Access database for 4 different categories of membership, which the visitor to the page selects (corporate, institutional, regular, or student). The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.