473,395 Members | 1,872 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,395 software developers and data experts.

getting results into one variable

hi
the database "execute" function returns a list of logical results. Each
logical result is a list of row tuples, as explained in the documents.

everytime i use it to execute various statements, it returns me, for
example
([(0,)], [(0,)], [(0,)]) and sometimes , ([(0,)], [(0,)]) or ([(0,)])

in my call, i give
eg (a,b,c) = db.execute(stmt) so that it returns me ([(0,)], [(0,)],
[(0,)])

in python, can we do something like

a = db.execute(stmt) and then expand variable 'a'

instead of doing
(a,b) = db.execute(stmt) for return of 2
(a,b,c) = for return of 3
(a,b,c,d) for return of 4

thanks

Nov 10 '05 #1
4 1851

s99999999s2...@yahoo.com wrote:
a = db.execute(stmt) and then expand variable 'a'

instead of doing
(a,b) = db.execute(stmt) for return of 2
(a,b,c) = for return of 3
(a,b,c,d) for return of 4


What do you intend to do with a, b, c,d ?

a = f(x)

always work. You can later then do a :

if len(a) == 1: single value
elif len(a) == 2: double value
....

Nov 10 '05 #2
s9************@yahoo.com wrote:

That's a nice email address :-)
hi
the database "execute" function returns a list of logical results. Each
logical result is a list of row tuples, as explained in the documents.
In a DB-API-compliant module, execution of the query adn retrieval of
the result(s) are actually sepearated: execute() executesthe query (and
on some, but not all, platforms returns the number of rows in the
result). Then you use either fetchone(), fetchmany() or fetchall() to
retrive the results from the cursor.
everytime i use it to execute various statements, it returns me, for
example
([(0,)], [(0,)], [(0,)]) and sometimes , ([(0,)], [(0,)]) or ([(0,)])
What you seem to be saying here is that you are getting a tuple of
lists, each of which contains a (single-element) tuple. What mopdule are
you using to do this, or is it the result of a gedanken-experiment?
in my call, i give
eg (a,b,c) = db.execute(stmt) so that it returns me ([(0,)], [(0,)],
[(0,)])

in python, can we do something like

a = db.execute(stmt) and then expand variable 'a'

instead of doing
(a,b) = db.execute(stmt) for return of 2
(a,b,c) = for return of 3
(a,b,c,d) for return of 4

thanks

Yes. Here's a pracical example using the database that generates
www.holdenweb.com:
import mx.ODBC.Windows as db
conn = db.connect("comsite")
curs = conn.cursor()
curs.execute("SELECT secID, secTitle, secPath FROM SECTION")

[Note that this returns None for this particular combination of database
module and backend].
rows = curs.fetchall()
rows [(1, 'Explore Holden Web', 'hd_explore'), (2, 'Student Links',
'hd_students'), (3, 'Other Stuff', 'hd_otherstuff'), (4, 'Recent Python
News', 'hd_pythonnews'),(5, 'Python Links', 'hd_pythonlinks'), (6,
'Python Reading', 'hd_pythonreading'), (7, 'Python Modules',
'hd_pythonreviews')]
You see here that fetchall() returns a list of tuples - each tuple being
a rows from the query result. It's normal to iterate over this list, and
one way to do this is:
for row in rows: ... print row
...
(1, 'Explore Holden Web', 'hd_explore')
(2, 'Student Links', 'hd_students')
(3, 'Other Stuff', 'hd_otherstuff')
(4, 'Recent Python News', 'hd_pythonnews')
(5, 'Python Links', 'hd_pythonlinks')
(6, 'Python Reading', 'hd_pythonreading')
(7, 'Python Modules', 'hd_pythonreviews')
Of course you can unpack each row if you want to refer to the columns
individually:
for row in rows: ... id, title, path = row
... print title, id
...
Explore Holden Web 1
Student Links 2
Other Stuff 3
Recent Python News 4
Python Links 5
Python Reading 6
Python Modules 7 You can save yourself some time by doing the unpacking right in the for
loop:
for id, title, path in rows: ... print id, title
...
1 Explore Holden Web
2 Student Links
3 Other Stuff
4 Recent Python News
5 Python Links
6 Python Reading
7 Python Modules
Finally, if you only want to use the result once you don't even need to
save it:
curs.execute("SELECT secID, secTitle, secPath FROM SECTION")
for id, ttl, pth in curs.fetchall(): ... print pth, ":", ttl
...
hd_explore : Explore Holden Web
hd_students : Student Links
hd_otherstuff : Other Stuff
hd_pythonnews : Recent Python News
hd_pythonlinks : Python Links
hd_pythonreading : Python Reading
hd_pythonreviews : Python Modules
You can use fetchone() to return each row as a tuple if that suits you
better, but it may be less efficient because it can lead to inefficient
communication between the database server and the client, particularly
if the result set is large.
curs.execute("SELECT secID, secTitle, secPath FROM SECTION")
curs.fetchone() (1, 'Explore Holden Web', 'hd_explore')
So of course you can unpack the tuple as well:
id, ttl, pth = curs.fetchone()
print "Title:", ttl, "path:", pth, "id:", id Title: Student Links path: hd_students id: 2


If the result sets are too large to comfortably hold in memory you can
fetch them N at a time with fetchmany(N), repeating until there's
nothing left to read. And so on, but I hope this gives you the idea.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 10 '05 #3
s9************@yahoo.com wrote:
hi (snip)
in python, can we do something like

a = db.execute(stmt) and then expand variable 'a'
instead of doing
(a,b) = db.execute(stmt) for return of 2
(a,b,c) = for return of 3
(a,b,c,d) for return of 4

Did you try ?-) Took me about 30'':
def fun(): return 1,2,3 .... a = fun()
a (1, 2, 3) def fun2(): return 1,2,3,4,8,9 .... b = fun2()
b (1, 2, 3, 4, 8, 9)
It of course works since a function *always* returns a *single* value.
Now this value *can* be a sequence, and this sequence *can* be unpacked
- directly at the return of the function, or latter:
a = fun()
a (1, 2, 3) v1, v2, v3 = a
v1 1 v2 2 v3 3


HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 10 '05 #4

Steve Holden wrote:
s9************@yahoo.com wrote:

That's a nice email address :-)
hi
the database "execute" function returns a list of logical results. Each
logical result is a list of row tuples, as explained in the documents.

In a DB-API-compliant module, execution of the query adn retrieval of
the result(s) are actually sepearated: execute() executesthe query (and
on some, but not all, platforms returns the number of rows in the
result). Then you use either fetchone(), fetchmany() or fetchall() to
retrive the results from the cursor.
everytime i use it to execute various statements, it returns me, for
example
([(0,)], [(0,)], [(0,)]) and sometimes , ([(0,)], [(0,)]) or ([(0,)])

What you seem to be saying here is that you are getting a tuple of
lists, each of which contains a (single-element) tuple. What mopdule are
you using to do this, or is it the result of a gedanken-experiment?
in my call, i give
eg (a,b,c) = db.execute(stmt) so that it returns me ([(0,)], [(0,)],
[(0,)])

in python, can we do something like

a = db.execute(stmt) and then expand variable 'a'

instead of doing
(a,b) = db.execute(stmt) for return of 2
(a,b,c) = for return of 3
(a,b,c,d) for return of 4

thanks

Yes. Here's a pracical example using the database that generates
www.holdenweb.com:
>>> import mx.ODBC.Windows as db
>>> conn = db.connect("comsite")
>>> curs = conn.cursor()
>>> curs.execute("SELECT secID, secTitle, secPath FROM SECTION")
>>>
[Note that this returns None for this particular combination of database
module and backend].
>>> rows = curs.fetchall()
>>> rows [(1, 'Explore Holden Web', 'hd_explore'), (2, 'Student Links',
'hd_students'), (3, 'Other Stuff', 'hd_otherstuff'), (4, 'Recent Python
News', 'hd_pythonnews'),(5, 'Python Links', 'hd_pythonlinks'), (6,
'Python Reading', 'hd_pythonreading'), (7, 'Python Modules',
'hd_pythonreviews')] >>>
You see here that fetchall() returns a list of tuples - each tuple being
a rows from the query result. It's normal to iterate over this list, and
one way to do this is:
>>> for row in rows: ... print row
...
(1, 'Explore Holden Web', 'hd_explore')
(2, 'Student Links', 'hd_students')
(3, 'Other Stuff', 'hd_otherstuff')
(4, 'Recent Python News', 'hd_pythonnews')
(5, 'Python Links', 'hd_pythonlinks')
(6, 'Python Reading', 'hd_pythonreading')
(7, 'Python Modules', 'hd_pythonreviews') >>>
Of course you can unpack each row if you want to refer to the columns
individually:
>>> for row in rows: ... id, title, path = row
... print title, id
...
Explore Holden Web 1
Student Links 2
Other Stuff 3
Recent Python News 4
Python Links 5
Python Reading 6
Python Modules 7 >>> You can save yourself some time by doing the unpacking right in the for
loop:
>>> for id, title, path in rows: ... print id, title
...
1 Explore Holden Web
2 Student Links
3 Other Stuff
4 Recent Python News
5 Python Links
6 Python Reading
7 Python Modules >>>
Finally, if you only want to use the result once you don't even need to
save it:
>>> curs.execute("SELECT secID, secTitle, secPath FROM SECTION")
>>> for id, ttl, pth in curs.fetchall(): ... print pth, ":", ttl
...
hd_explore : Explore Holden Web
hd_students : Student Links
hd_otherstuff : Other Stuff
hd_pythonnews : Recent Python News
hd_pythonlinks : Python Links
hd_pythonreading : Python Reading
hd_pythonreviews : Python Modules >>>
You can use fetchone() to return each row as a tuple if that suits you
better, but it may be less efficient because it can lead to inefficient
communication between the database server and the client, particularly
if the result set is large.
>>> curs.execute("SELECT secID, secTitle, secPath FROM SECTION")
>>> curs.fetchone() (1, 'Explore Holden Web', 'hd_explore') >>>
So of course you can unpack the tuple as well:
>>> id, ttl, pth = curs.fetchone()
>>> print "Title:", ttl, "path:", pth, "id:", id Title: Student Links path: hd_students id: 2 >>>


If the result sets are too large to comfortably hold in memory you can
fetch them N at a time with fetchmany(N), repeating until there's
nothing left to read. And so on, but I hope this gives you the idea.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


thanks , that's excellent..
currently what i do is sort of combined the sql statements, something
like

stmt = """declare @res int
exec @res sp_adduser '%s'
if @res = 0
insert table values...blah blah where col = '%s'
""" % ( login, colvalue)

then when i do
(a,b) = db.executed(stmt), it gives me ([(0,)], [(1,)]) when i print
(a,b)
i think one result is the one from sp_adduser stored proc and the other
is the insertion.
if i want to get a or b's value, i would do a[0][0] or b[0][0]

will using a cursor with fetch* give me a return result of a stored
proc...?
i did not use cursors for executing stored procs...i just call
db.execute(stmt) , from the eg above.

i think i am going to revamp some of my codes, after looking at your
reply. thanks

Nov 10 '05 #5

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

Similar topics

2
by: Wm | last post by:
This is very peculiar -- for some reason, I'm getting 6-8 results from each of these queries, although only one listing matches. I have a pair of forms on one page: <FORM> Search for lastname:...
17
by: John Hunter | last post by:
I have a largish data set (1000 observations x 100 floating point variables), and some of the of the data are missing. I want to try a variety of clustering, neural network, etc, algorithms on the...
0
by: Golawala, Moiz M (GE Infrastructure) | last post by:
Hi All, Could someone please help me with an issue I am having. I am having problem returning values from a Stored Procedure that creates a dynamic table (table variable) inserts values during a...
4
by: Rajesh Kumar Mallah | last post by:
Hi, We need to implement following logic efficiently. SELECT * from some_table where .... IF rows_matched = 1 THEN use the single row that matched.
22
by: SF | last post by:
Hello All, In a windows C learning project I am wokring on I use the system function to run a command, I want to suck the results into a vairable. The system function seems to only return an...
9
by: christopher_board | last post by:
Hi all. I am trying to write a php page which connects to a MySQL Database which is supposed to get the results from a table within a database and display the results in a table. Below is the...
4
by: meendar | last post by:
Hi to all, I just need to get two fields from a table and manipulate the results in next query of a procedure.I planned to code like what you see below, create procedure marks1 as @ sql1 as...
0
by: tharika_c | last post by:
Hi, We have a simple ASP.NET web application where one of the Session variables, called Session("SSO_ID") gets created and assigned a value (equal to the HTTP_HRID request variable value),...
3
by: kevenj | last post by:
Hello, I'm a bit of a newcomer to PHP and am having an issue I can't overcome. I've read some great posts on the issue of grouping results on this forum 1 and can happily display results under...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
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...

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.