473,796 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

module for working with the result set

Hi,
newbie question:
PyPgSQL (postgres driver) returns a list (not a tuple O'Rielly states
DBAPI returns a tuple) and most of the books describe how to work with
tuples and dictionaries when working with a result set. Does anyone know
of a paper or tutorial that explains a few ways to deal with result
sets. Or if anyone knows of a module that will help (that I can study) -
that would be just as helpful?

TIA
John
Jul 18 '05 #1
15 1678
john fabiani wrote:
Hi,
newbie question:
PyPgSQL (postgres driver) returns a list (not a tuple O'Rielly states
DBAPI returns a tuple) and most of the books describe how to work with
tuples and dictionaries when working with a result set. Does anyone know
of a paper or tutorial that explains a few ways to deal with result
sets. Or if anyone knows of a module that will help (that I can study) -
that would be just as helpful?


What exactly is your problem? Lists behave equally with respect to read
accees to tuples. So whatever you want to do with a tuple, do it.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Diez B. Roggisch wrote:
john fabiani wrote:

Hi,
newbie question:
PyPgSQL (postgres driver) returns a list (not a tuple O'Rielly states
DBAPI returns a tuple) and most of the books describe how to work with
tuples and dictionaries when working with a result set. Does anyone know
of a paper or tutorial that explains a few ways to deal with result
sets. Or if anyone knows of a module that will help (that I can study) -
that would be just as helpful?

What exactly is your problem? Lists behave equally with respect to read
accees to tuples. So whatever you want to do with a tuple, do it.


That's what I thought but everytime I attempt to use any of the examples
in the O'Reilly books I get errors. I'm so new to this stuff I can't
tell if it's me or the fact I not using tuples. Things like the following:

mydata=cursor.f etchmany(10)
for field in mydata[0]:
print mydata[0][field]

Traceback (most recent call last):
File "C:\Python23\Li b\site-packages\sm\scr iptutils.py", line 49, in run
exec codeObject in mainDict
File "<source>", line 10, in ?
File "C:\Python23\Li b\site-packages\pyPgSQ L\PgSQL.py", line 2063, in
__getitem__
key = self._xlatkey[key.lower()]
KeyError: '5992 '
Exception raised while running script <source>

But ;(
print mydata[0][0], mydata[0][1]
the above works perfectly - prints what I want.

So I'm confused. Also, using things like the dtuple module does not
work at all.

thanks
John
Jul 18 '05 #3
john fabiani writes:
mydata=cursor.f etchmany(10)
for field in mydata[0]:
print mydata[0][field]


I think you want:

mydata=cursor.f etchmany(10)

for record in mydata:
print "--- record ---"
for field in record:
print field
Jul 18 '05 #4
> mydata=cursor.f etchmany(10)
for field in mydata[0]:
print mydata[0][field]

Traceback (most recent call last):
File "C:\Python23\Li b\site-packages\sm\scr iptutils.py", line 49, in run
exec codeObject in mainDict
File "<source>", line 10, in ?
File "C:\Python23\Li b\site-packages\pyPgSQ L\PgSQL.py", line 2063, in
__getitem__
key = self._xlatkey[key.lower()]
KeyError: '5992 '
Exception raised while running script <source>

But ;(
print mydata[0][0], mydata[0][1]
the above works perfectly - prints what I want.


it seems that the value of field is 5992, and that isn't a key to the row -
which is quite likely, as 5992 as column-name is most probably forbidden,
and as index a little bit high - can't imagine what a table with ~6000
columns should hold....

Now what about showing us the result of this:

mydata=cursor.f etchmany(10)
print mydata[0]

Then one can tell how to access the fields. Thats a general advice: when
such things occur, print out the values in question short before the
problem, then you can see if and more important why what you expect and
what you get differ.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #5
Try:
...
for coldesc in cursor.descript ion:
print coldesc[0],
print
for row in cursor.fetchman y(10):
print row
....

--
-Scott David Daniels
Sc***********@A cm.Org
Jul 18 '05 #6
Diez B. Roggisch wrote:
mydata=cursor .fetchmany(10)
for field in mydata[0]:
print mydata[0][field]

Traceback (most recent call last):
File "C:\Python23\Li b\site-packages\sm\scr iptutils.py", line 49, in run
exec codeObject in mainDict
File "<source>", line 10, in ?
File "C:\Python23\Li b\site-packages\pyPgSQ L\PgSQL.py", line 2063, in
__getitem__
key = self._xlatkey[key.lower()]
KeyError: '5992 '
Exception raised while running script <source>

But ;(
print mydata[0][0], mydata[0][1]
the above works perfectly - prints what I want.

it seems that the value of field is 5992, and that isn't a key to the row -
which is quite likely, as 5992 as column-name is most probably forbidden,
and as index a little bit high - can't imagine what a table with ~6000
columns should hold....

Now what about showing us the result of this:

mydata=cursor.f etchmany(10)
print mydata[0]

Then one can tell how to access the fields. Thats a general advice: when
such things occur, print out the values in question short before the
problem, then you can see if and more important why what you expect and
what you get differ.

See this does not make sense to me.
mydata[0]
['5992 ', 'A', '5346 ', ' ', 'XX
', 'Kathy Stromme ', 'RICHMOND AMERICAN HOMES
', '7250 WEST PEAK, ST # 212 ', '
', 'LAS VEGAS ', 'NEVADA
', '89128 ', ' ', '702-240-5600
', ' ', 'RICHMOND AMERICAN HOMES
', '7250 WEST PEAK, ST # 212 ', '
', 'LAS VEGAS ', 'NEVADA ',
'89128 ', ' ', '702-240-5600 ', '
', ' ', ' ', '
', ' ', ' ', ' ', ' ', '
', '0/0/30 ', 'FIRST ', ' ', '
', ' ', ' ', '
', 'USD', ' ', <DateTime object for '2004-01-16
00:00:00.00' at 1779820>, <DateTime object for '2004-02-06 00:00:00.00'
at 17590e0>, <PgBoolean instance at 01EBB580: Value: False>, <PgBoolean
instance at 01EBB580: Value: False>, <PgBoolean instance at 01EBB580:
Value: False>, <PgBoolean instance at 01EBB580: Value: False>,
<PgBoolean instance at 01EBB580: Value: False>, <PgBoolean instance at
01EBB580: Value: False>, <PgBoolean instance at 01EBB580: Value: False>,
<PgBoolean instance at 01EBB580: Value: False>, <PgBoolean instance at
01EBB580: Value: False>, <PgBoolean instance at 01EBB580: Value: False>,
<PgBoolean instance at 01EBB580: Value: False>, <PgNumeric instance -
precision: 3 scale: 0 value: 0>, <PgNumeric instance - precision: 3
scale: 0 value: 30>, <PgNumeric instance - precision: 6 scale: 2 value:
0.00>, <PgNumeric instance - precision: 6 scale: 2 value: 0.00>,
<PgNumeric instance - precision: 8 scale: 5 value: 0.00000>, <PgNumeric
instance - precision: 8 scale: 5 value: 0.00000>, <PgNumeric instance -
precision: 8 scale: 5 value: 0.00000>, <PgNumeric instance - precision:
14 scale: 2 value: 0.00>, <PgNumeric instance - precision: 14 scale: 2
value: 0.00>, <PgNumeric instance - precision: 14 scale: 2 value: 0.00>,
<PgNumeric instance - precision: 14 scale: 2 value: 0.00>, <PgNumeric
instance - precision: 14 scale: 2 value: 0.00>, <PgNumeric instance -
precision: 14 scale: 2 value: 0.00>, <PgNumeric instance - precision: 14
scale: 2 value: 0.00>, <PgNumeric instance - precision: 14 scale: 2
value: 0.00>, <PgNumeric instance - precision: 14 scale: 2 value: 0.00>,
<PgNumeric instance - precision: 14 scale: 2 value: 0.00>, <PgNumeric
instance - precision: 14 scale: 2 value: 0.00>, <PgNumeric instance -
precision: 14 scale: 2 value: 0.00>, <PgNumeric instance - precision: 14
scale: 2 value: 0.00>, <PgNumeric instance - precision: 14 scale: 2
value: 0.00>, <PgNumeric instance - precision: 14 scale: 2 value: 0.00>,
<PgNumeric instance - precision: 14 scale: 2 value: 0.00>, <PgNumeric
instance - precision: 14 scale: 2 value: 0.00>, <PgNumeric instance -
precision: 14 scale: 2 value: 0.00>, <PgNumeric instance - precision: 14
scale: 6 value: 1.000000>, <PgNumeric instance - precision: 14 scale: 2
value: 0.00>, <PgNumeric instance - precision: 14 scale: 2 value: 0.00>,
<PgNumeric instance - precision: 14 scale: 2 value: 0.00>, <PgNumeric
instance - precision: 14 scale: 2 value: 0.00>, <PgNumeric instance -
precision: 14 scale: 2 value: 0.00>, '', '', '64 ', ' ',
'4-PPPP-34 ', 'COURTYARD @ LONE MT. TERRACE ', ' ', 'RACL
', ' ',
'R1', ' ', 'ALVJU', 'MONJO', <PgNumeric instance -
precision: 2 scale: 0 value: 30>, <PgNumeric instance - precision: 7
scale: 2 value: 36.48>, <PgNumeric instance - precision: 5 scale: 2
value: 25.00>, <PgNumeric instance - precision: 5 scale: 2 value: 0.00>,
<PgNumeric instance - precision: 7 scale: 2 value: 14.42>, <PgNumeric
instance - precision: 5 scale: 2 value: 0.00>, <PgNumeric instance -
precision: 5 scale: 2 value: 0.00>, None, None, None, <PgNumeric
instance - precision: 5 scale: 2 value: 2.30>, None, <PgNumeric instance
- precision: 10 scale: 2 value: 317.00>, <PgNumeric instance -
precision: 10 scale: 1 value: 5346.4>, 'JUAN ALVAREZ
', 'JOSE MONTALVO
', ' ', '5992 ', ' ', None, None, <PgNumeric
instance - precision: 7 scale: 2 value: 0.00>, <PgNumeric instance -
precision: 7 scale: 2 value: 0.00>, None, None, '
', '
', None, None, None, None, <PgBoolean instance at 01EBB580:
Value: False>, ' ', <DateTime object for '2004-02-12
00:00:00.00' at 1759060>, ' ', None,
None, None, ' ', None, <PgBoolean instance at
01EBB580: Value: False>]
Jul 18 '05 #7
Paul McNett wrote:
john fabiani writes:

mydata=cursor .fetchmany(10)
for field in mydata[0]:
print mydata[0][field]

I think you want:

mydata=cursor.f etchmany(10)

for record in mydata:
print "--- record ---"
for field in record:
print field

Hi Paul,

What I'd like to see is the data. So I can do
Scan
list the rows somewhat like a browse window.

endscan

But I don't even understand what is returned. The first part appears
perfect but the rest who knows.

John
Jul 18 '05 #8
john fabiani wrote:
See this does not make sense to me.
mydata[0]
['5992 ', 'A', '5346 ', ' ', 'XX
', 'Kathy Stromme ', 'RICHMOND AMERICAN HOMES
', '7250 WEST PEAK, ST # 212 ', '


<snip>

well - its a list, and I bet its a list of columns. The columns are mostly
strings (at least at the beginning) and obviously are space padded to match
up to their size.

The code you posted earlier assumes that the result row is returned as dict,
thus iterating over the row yields in the keys. Consider this example:

d = {"key1" : 10, "key2" : 20 }
for key in d:
print key, d[key]

that results in

key1
10
key2
20

notice how putting a dict as sequence in a for ... in ... : statement
iterates the keys.

Now you don't get a dict, but a list of row-values. So your code

for field in mydata[0]:
***print*mydata[0][field]

iterates over the data you presented, yielding the '5992******'
as first value. Thats all. Of course using that value as index makes no
sense.

To summarize: you expect data to be returned as dict per row, where the data
is returned as list.

Use the cursors .description property to get a mapping between name and
index.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #9
Diez B. Roggisch wrote:
john fabiani wrote:

See this does not make sense to me.
mydata[0]
['5992 ', 'A', '5346 ', ' ', 'XX
', 'Kathy Stromme ', 'RICHMOND AMERICAN HOMES
', '7250 WEST PEAK, ST # 212 ', '

<snip>

well - its a list, and I bet its a list of columns. The columns are mostly
strings (at least at the beginning) and obviously are space padded to match
up to their size.

The code you posted earlier assumes that the result row is returned as dict,
thus iterating over the row yields in the keys. Consider this example:

d = {"key1" : 10, "key2" : 20 }
for key in d:
print key, d[key]

that results in

key1
10
key2
20

notice how putting a dict as sequence in a for ... in ... : statement
iterates the keys.

Now you don't get a dict, but a list of row-values. So your code

for field in mydata[0]:
print mydata[0][field]

iterates over the data you presented, yielding the '5992 '
as first value. Thats all. Of course using that value as index makes no
sense.

To summarize: you expect data to be returned as dict per row, where the data
is returned as list.

Use the cursors .description property to get a mapping between name and
index.

OK thats great! I did the following and it works.
for field in range(0,len(myd ata[0])):
print mydata[0][field]
following your thoughts on the keys and my mistake - can you tell me how
to get into a dict format?

John
Jul 18 '05 #10

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

Similar topics

1
2602
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
2
1995
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for my wrapper. This is acually part of the project, libxmlconf on sourceforge. The newest working version isn't there yet, and cvs is lagged by 6 hours or so. So if you think you want to have a try at this I can tgz the source for you. My...
0
1363
by: Jeff | last post by:
Ok, so I'm working on a project right now that is being done in psp through apache. This is working fine, except that in every one of my modules I had to use the mod_python function of apache.import_module for its autoreload capability. Now I cannot open or run any of my classes in the interpreter without getting a "No module named _apache" error. I need to get this working, as I am without unittesting right now. I also tried making...
1
4185
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
0
1231
by: Dave Cole | last post by:
WHAT IS IT: The Sybase module provides a Python interface to the Sybase relational database system. It supports all of the Python Database API, version 2.0 with extensions. NOTES: This release contains a number of small bugfixes and patches received from users.
0
1313
by: Dave Cole | last post by:
WHAT IS IT: The Sybase module provides a Python interface to the Sybase relational database system. It supports all of the Python Database API, version 2.0 with extensions. NOTES: This release contains a number of small bugfixes and patches received from users.
0
1198
by: Dave Cole | last post by:
WHAT IS IT: The Sybase module provides a Python interface to the Sybase relational database system. It supports all of the Python Database API, version 2.0 with extensions. NOTES: The 0.37 release is identical to 0.37pre3 as no problems were reported with the prerelease.
2
2752
by: Mythran | last post by:
I'm trying to add a module template to a project programmatically. I've tried many documented (w/o examples) ways of doing it to no avail. For example, I tried using Project.ProjectItems.AddFromTemplate("C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBWizards\Module\Templates\1033\Module.vb", "Common.vb") and this adds it ok. Problem is, it does not replace the stays as such). So I tried the DTE.LaunchWizard method and passed...
1
17262
by: alain MONTMORY | last post by:
Hello everybody, I am a newbie to python so I hope I am at the right place to expose my problem..... :-http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding I download the code example from http://www.python.org/doc/2.4.2/ext/run-func.txt I call the file "TestOfficiel.c" and I compile it with : gcc -g -I/usr/include/python2.3/ TestOfficiel.c -o TestOfficiel -lpython2.3 -ldl all is OK (or seems to be...).
0
9684
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.