[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

MySQLdb and column names

beef

1/30/2008 5:04:00 PM

Hello all,

I am using MySQLdb 1.2.2 and have a question about the construction of
the dictionary keys of a result set.

Here is an example query, from which you may intuit some of the
structure of the tables:

SELECT
shots.*,
users.*,
sequences.*,
jobs.*
FROM
shots
LEFT JOIN
users ON users.id=shots.user_id
INNER JOIN
sequences ON sequences.id=shots.sequence_id
INNER JOIN
jobs AS j ON j.id=sequences.job_id
WHERE
shots.id=%s

1. The 'users' table has a column named 'id', as do all the other
tables.
2. I define my cursor as a 'DictCursor' so that my results are
dictionaries
3. The 'shots' id has the key name of 'id', while all the -other-
'id's have key names of the form: "<TABLE>.id"

I would prefer to have the key names consistent, so that event the
"shots" fields have key names of the form "<TABLE>.id"

Is this possible?

Thanks in advance!

--
Wellington

2 Answers

beef

1/30/2008 5:11:00 PM

0

change:
I would prefer to have the key names consistent, so that event the

to:
I would prefer to have the key names consistent, so that *even* the

Dennis Lee Bieber

1/30/2008 6:11:00 PM

0

On Wed, 30 Jan 2008 09:04:26 -0800 (PST), beef <winterbeef@gmail.com>
declaimed the following in comp.lang.python:


> SELECT
> shots.*,
> users.*,
> sequences.*,
> jobs.*
> FROM
> shots
> LEFT JOIN
> users ON users.id=shots.user_id
> INNER JOIN
> sequences ON sequences.id=shots.sequence_id
> INNER JOIN
> jobs AS j ON j.id=sequences.job_id
> WHERE
> shots.id=%s
>
> 3. The 'shots' id has the key name of 'id', while all the -other-
> 'id's have key names of the form: "<TABLE>.id"
>
The <TABLE> prefix is probably being used when the query processor
discovers that there are duplicate field names. IE, it is just
processing the table descriptions from left to right, and when a
subsequent table has the same field name as one that has already been
used, it prefaces the second (third, ...) with the source table.

> I would prefer to have the key names consistent, so that event the
> "shots" fields have key names of the form "<TABLE>.id"
>
Suggest, first step, do NOT use "table.*" notation, but rather
explicitly list the fields you want from each table, in the order you
want them. That, alone, might add the table names to all fields, though
I suspect not -- so use an "as xxx" renaming of the fields you need to
differentiate.

... shots.id as "shots.id", other.fields, other.tables...
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/