Re: [Tutor] Fetching dictionaries using MySQLdb

2005-08-08 Thread Jan Eden
Hi Danny,

Danny Yoo wrote on 08.08.2005:

>On Mon, 8 Aug 2005, Jan Eden wrote:
>
>>Is there a recommended way to receive the results of an SQL query
>>in the form I need? Or do I have to create a dictionary of
>>fieldname tuples which can be zipped with the query result tuple?
>
>
>Hi Jan,
>
>MySQLdb supports the concept of customized cursor types.  They have
>a particular cursor class that returns dictionary objects:

Great! I found the pydoc for MySQLdb a bit confusing and would probably have 
taken quite a while to figure that out - thanks a lot.

- Jan
--  
Any technology which is distinguishable from magic is insufficiently advanced.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fetching dictionaries using MySQLdb

2005-08-08 Thread Danny Yoo


On Mon, 8 Aug 2005, Jan Eden wrote:

> Is there a recommended way to receive the results of an SQL query in the
> form I need? Or do I have to create a dictionary of fieldname tuples
> which can be zipped with the query result tuple?


Hi Jan,

MySQLdb supports the concept of customized cursor types.  They have a
particular cursor class that returns dictionary objects:

##
>>> import MySQLdb
>>> import MySQLdb.cursors
>>> MySQLdb.cursors.DictCursor

##


When we construct a connection object, we can tell MySQL to construct
those kind of custom cursors by default:

##
>>> conn = MySQLdb.connect(db='test_adb',
...cursorclass=MySQLdb.cursors.DictCursor)
>>> cursor = conn.cursor()
>>> cursor.execute("""select name, start_coordinate,
...end_coordinate
...   from BAC""")
1624L
>>>
>>> cursor.fetchone()
{'name': 'F10A5', 'end_coordinate': 28462975L, 'start_coordinate':
28333429L}
>>> cursor.fetchone()
{'name': 'T12M4', 'end_coordinate': 3002250L, 'start_coordinate':
2942990L}
>>> cursor.fetchone()
{'name': 'T12I7', 'end_coordinate': 24869607L, 'start_coordinate':
24834300L}
##


Good luck!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fetching dictionaries using MySQLdb

2005-08-08 Thread Jan Eden
Kent Johnson wrote on 08.08.2005:

>Jan Eden wrote:

>>The documentation for MySQLdb says that fetchoneDict() is
>>deprecated and the usage of fetchone() is suggested.
>
>You could just use fetchoneDict(); deprecated isn't the same as
>gone...

I tend to avoid deprecated functions/methods - I would need another method 
sooner or later anyway.
>>
>>Is there a recommended way to receive the results of an SQL query
>>in the form I need? Or do I have to create a dictionary of
>>fieldname tuples which can be zipped with the query result tuple?
>
>You can create a list of field names from the cursor.description
>attribute. But if the number of fields in the result varies how do
>you know which field goes with which description?
>
That works, thank you. I will use the cursor.description attribute immediately 
after executing a query, so it will contain the correct field descriptions 
whenever I need them. The rest of my program uses the type attribute to decide 
which fields are present and which are not.

All this is part of a port from Perl to Python - I will publish a log of 
everything I encountered in a couple of weeks.

Cheers,

Jan
-- 
There's no place like ~/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fetching dictionaries using MySQLdb

2005-08-08 Thread Kent Johnson
Jan Eden wrote:
> Hi,
> 
> in Perl's DBI, I used fetchrow_hashref() to receive a database row as
> a dictionary, with the field names being the dictionary keys.
> 
> MySQLdb's fetchone() returns a tuple Unfortunately, I have a
> dictionary of SQL queries which return rows of different lengths
> (read: with a varying number of fields).
> 
> The documentation for MySQLdb says that fetchoneDict() is deprecated
> and the usage of fetchone() is suggested.

You could just use fetchoneDict(); deprecated isn't the same as gone...
> 
> Is there a recommended way to receive the results of an SQL query in
> the form I need? Or do I have to create a dictionary of fieldname
> tuples which can be zipped with the query result tuple?

You can create a list of field names from the cursor.description attribute. But 
if the number of fields in the result varies how do you know which field goes 
with which description?

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fetching dictionaries using MySQLdb

2005-08-08 Thread Jan Eden
Hi,

in Perl's DBI, I used fetchrow_hashref() to receive a database row as a 
dictionary, with the field names being the dictionary keys.

MySQLdb's fetchone() returns a tuple Unfortunately, I have a dictionary of SQL 
queries which return rows of different lengths (read: with a varying number of 
fields).

The documentation for MySQLdb says that fetchoneDict() is deprecated and the 
usage of fetchone() is suggested.

Is there a recommended way to receive the results of an SQL query in the form I 
need? Or do I have to create a dictionary of fieldname tuples which can be 
zipped with the query result tuple?

Thanks,

Jan
-- 
Any sufficiently advanced technology is indistinguishable from a Perl script. - 
Programming Perl
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor