473,412 Members | 2,088 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,412 software developers and data experts.

Databases: Getting values by column name

Hi,

I can't find any good documentation about the Python-database module, all I
found was this rudimentary (no examples, no references) piece here:

http://www.python.org/peps/pep-0249.html

Anyway, this and also this tutorial here:

http://www.devshed.com/c/a/Python/My...With-Python/2/

only use database rows as sequences, ie as arrays with numerical index.
This gives you loads of problems when using "select *" and also bad code
readability when selecting specific columns.

Is there a way to use them as dictionaries, ie with Index-Strings (=column
names) as indexes?

BTW, is there any good searchable reference to Python's functions, with
examples and cross-references to similar functions?

Thanks a lot,

Robert
Jul 18 '05 #1
7 2630

Robert> http://www.devshed.com/c/a/Python/My...With-Python/2/

Robert> only use database rows as sequences, ie as arrays with numerical
Robert> index. This gives you loads of problems when using "select *"
Robert> and also bad code readability when selecting specific columns.

Robert> Is there a way to use them as dictionaries, ie with
Robert> Index-Strings (=column names) as indexes?

Yes, for MySQLdb import the MySQLdb.cursors module then set the cursorclass
arg when you create a connection, like so:

import MySQLdb
import MySQLdb.cursors
...
conn = MySQLdb.Connection(host=...,
user=...,
passwd=...,
db=...,
cursorclass=MySQLdb.cursors.DictCursorNW,
...)

I think the reason this sort of behavior isn't spelled out in PEP 249 is
because it varies too much from one database to another. Psycopg (a
PostgreSQL adaptor) does it differently. Instead of specifying the cursor
type when the connection is created, you instantiate a different cursor
class:

conn = psycopg.connect("...")
...
cursor = conn.dictcursor()

I imagine there are some databases that either don't support name-based
retrieval very well (or at all), or make it difficult to get at. You might
get a more solid response on the reasons for this omission from PEP 249 from
the db-sig folks:

http://www.python.org/sigs/db-sig/

Skip
Jul 18 '05 #2
Skip Montanaro wrote:
Yes, for MySQLdb import the MySQLdb.cursors module then set the
cursorclass arg when you create a connection, like so:

import MySQLdb
import MySQLdb.cursors
...
conn = MySQLdb.Connection(host=...,
user=...,
passwd=...,
db=...,
cursorclass=MySQLdb.cursors.DictCursorNW,
...)
You mean MySQLdb.connect()?
I think the reason this sort of behavior isn't spelled out in PEP 249 is
because it varies too much from one database to another. Psycopg (a
PostgreSQL adaptor) does it differently. Instead of specifying the cursor
type when the connection is created, you instantiate a different cursor
class:

conn = psycopg.connect("...")
NameError: name 'psycopg' is not defined

Also wouldn't that overwrite the above conn variable?
...
cursor = conn.dictcursor()

AttributeError: Connection instance has no attribute 'dictcursor'

(When I use the conn from MySQLdb.connect)
I imagine there are some databases that either don't support name-based
retrieval very well (or at all), or make it difficult to get at. You
might get a more solid response on the reasons for this omission from PEP
249 from the db-sig folks:

http://www.python.org/sigs/db-sig/


Isn't there any documentation with a simple example out there?

I already know the stuff on that page, it's really slowing you down :-(

Robert
Jul 18 '05 #3
Robert Ferber wrote:

I already know the stuff on that page, it's really slowing you down :-(


Your response to Skip Montaro's post demonstrates that
you zipped trough it without paying much attention to what
he said, so I think slowing a bit down would actually help
you here.

As other pointed out you have everything you need, using dtuple.py at:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/81252

or db_row at:

http://opensource.theopalgroup.com/

Istvan.
Jul 18 '05 #4
Istvan Albert wrote:
Robert Ferber wrote:

I already know the stuff on that page, it's really slowing you down :-(
Your response to Skip Montaro's post demonstrates that
you zipped trough it without paying much attention to what
he said, so I think slowing a bit down would actually help
you here.


I am already looking for that solution for several days already, I'm quite a
bit frustrated by now, sorry for my inpoliteness.

However Skip's solution almost works, at least the import statements and the
connect statement with cursorclass=MySQLdb.cursors.DictCursorNW don't
report an error.
As other pointed out you have everything you need, using dtuple.py at:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/81252
Of course I looked at that webpage, but if I'm not mistaken, mx.ODBC.Windows
is only available on Windows which makes it useless for me, I can not find
a ODBC Package for my Linux distribution and the webpage doesn't give the
slightest hint what the package name is (which probably would make
searching for it too easy) or where to get it.

My distribution (SUSE) comes with dozens of Python-packages, but none have
"mx" or "odbc" in their name.

So after wasting another 20 minutes I discarded that webpage as useless and
responded to the only post that looked promising which was Skip's.
or db_row at:

http://opensource.theopalgroup.com/


http://opensource.theopalgroup.com/files/ADOdb.html
Purpose: Python DB-API 2.0 interface to Microsoft ADO objects
Compatibility: Python 2.0
Requires: Python Win32 extensions, ADO 2.5+, mx.DateTime
Revision: Version 0.10 - Alpha quality code

I didn't know that the Python-community was infested by so much
Microsoft-only code. Both of your hints seem to be Win-only. And it's
"Alpha quality".

Robert

Jul 18 '05 #5
>> conn = MySQLdb.Connection(host=...,

Robert> You mean MySQLdb.connect()?

MySQLdb has Connect, connect and Connection, all the same function.
MySQLdb.Connection <function Connect at 0xdc2930> MySQLdb.Connect <function Connect at 0xdc2930> MySQLdb.connect
<function Connect at 0xdc2930>
I think the reason this sort of behavior isn't spelled out in PEP 249
is because it varies too much from one database to another. Psycopg
(a PostgreSQL adaptor) does it differently. Instead of specifying
the cursor type when the connection is created, you instantiate a
different cursor class:

conn = psycopg.connect("...")
Robert> NameError: name 'psycopg' is not defined

Yes, you'd have to import it.

Robert> Also wouldn't that overwrite the above conn variable?
...
cursor = conn.dictcursor()


Robert> AttributeError: Connection instance has no attribute 'dictcursor'

Like I said, psycopg is an adapter for the PostgreSQL database. MySQLdb is
an adapter for the MySQL database. There are others as well. I thought
showing how the two I'm most familiar with in this arena differ would be
sufficient.

Robert> I already know the stuff on that page, it's really slowing you
Robert> down :-(

I don't understand what you're referring to here. What do you mean by
"slowing you down"?

Skip

Jul 18 '05 #6
Robert Ferber wrote:

As other pointed out you have everything you need, using dtuple.py at:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/81252
Of course I looked at that webpage, but if I'm not mistaken, mx.ODBC.Windows
is only available on Windows which makes it useless for me, I can not find
a ODBC Package for my Linux distribution and the webpage doesn't give the
slightest hint what the package name is (which probably would make
searching for it too easy) or where to get it.


The dtuple.py module is database independent and will work with just
about all database connectors.

The fact that the example uses mx.ODBC.Windows is completely
irrelevant it will work the same way with every other connector.
I didn't know that the Python-community was infested by so much
Microsoft-only code. Both of your hints seem to be Win-only. And it's
" Alpha quality".


Heh, while I would agree that the formatting on the page
is both slightly misleading and does not visually separate the
different downloads well, you'd save yourself a lot of aggravation
just by spending a little more time on each of these pages.

The module that I (and others) talked about is called db_row and
is located right above the on one you talk about (the windows
specific database *driver*).

Istvan.

Jul 18 '05 #7
Robert Ferber schrieb:
As other pointed out you have everything you need, using dtuple.py at:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/81252

Of course I looked at that webpage, but if I'm not mistaken, mx.ODBC.Windows
is only available on Windows which makes it useless for me, I can not find
a ODBC Package for my Linux distribution and the webpage doesn't give the
slightest hint what the package name is (which probably would make
searching for it too easy) or where to get it.


I can see no reason why the recipe shouldn't work just as well with
MySQLdb instead of mx.ODBC. And the docstrings for the dtuple classes
state clearly that they work with results "from a DB-API fetch*()
method". No mention of a specific package here.

In fact, the docstring for TupleDescriptor even says "Note: the term
database tuple is rather specific; in actuality the tuple may have come
from non-database sources and/or generated by a process wholly unrelated
to databases."
My distribution (SUSE) comes with dozens of Python-packages, but none have
"mx" or "odbc" in their name.

But it does come with MySQLdb IIRC.

HTH
Koczian
Jul 18 '05 #8

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

Similar topics

6
by: Michael | last post by:
Hi All, I need a few suggestions. I have the following XML segment: <LookUp> <ControlType>CheckBoxGroup</ControlType> <DBField>LastMedDate</DBField> <ControlName>cmbGoal1</ControlName> <Values...
1
by: edo | last post by:
Following is a summary of articles spanning a 7 day period, beginning at 12 Dec 2003 05:22:35 GMT and ending at 19 Dec 2003 04:09:06 GMT. Notes ===== - A line in the body of a post is...
3
by: Pasquale | last post by:
I have a database used for contact information and another for event information. As part of the event database, we would like to keep record of the people that are attending a certain event. These...
1
by: nate axtell | last post by:
In VB .NET I load the contents of an Excel or comma seperated values file into a dataGrid (via a datatable). One of the columns has a comma in the name of the column. So for the comma separated...
4
by: Ben | last post by:
So, at my place of employment, we use a national standard to transmit data between certain applications. This standard consists of a fixed width, flat file 4500-some-odd chars wide that contain...
3
by: itfetish | last post by:
I've been programming a bit in PHP and then recently learnt classic ASP for projects at work, now I'm working on another project that they want done in .net. I've got my head around web parts, that...
3
by: kbcompany | last post by:
Hi I have just started learning oracle. i have created a table with name imp_master and columns item_code with constraint not null, item_name with constraint n1 not null(n1 is user...
0
by: David | last post by:
On Wed, Jun 18, 2008 at 11:16 AM, M.-A. Lemburg <mal@egenix.comwrote: Thanks for your reply. How do you maintain foreign key references with this approach? eg, you have these 4 tables: ...
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.