un-tuple (newbie)

M

Martin Doering

Hi!


I want to use Jython to fetch exactly one value from a database query,
like here:

# execute a query
def query(db, sql):
c = db.cursor()
c.execute(sql)
for line in c.fetchall():
val = line
print val
c.close()
return val


I could find out, how to create a tuple with just one member, but I
can not find out how to get a value from a tuple with just one member.

It will be an easy answer, I think. Sorry, it all is totally new for
me. :)
 
H

Heather Coppersmith

# execute a query
def query(db, sql):
c = db.cursor()
c.execute(sql)
for line in c.fetchall():
val = line
print val
c.close()
return val
I could find out, how to create a tuple with just one member, but I
can not find out how to get a value from a tuple with just one member.

The tuple's only member is it's "zeroth" element:

print val[ 0 ]

HTH,
Heather
 
S

sdd

Martin said:
I want to use Jython to fetch exactly one value from a database query,
like here:

# execute a query
def query(db, sql):
c = db.cursor()
c.execute(sql)
for line in c.fetchall():
val = line
val, = line # one way
(val,) = line # same as above (perhaps more readable?)
val = line[0] # another way
print val
c.close()
return val

Personally, I'd call it "cursor", not "c". Also my DB predeliction
leads me to prefer calling it a "row", not a "line".

If you know (and don't care to check) that a single row is returned,
with a single value, I might try something like this:

def single_value_query(db, sql):
cursor = db.cursor()
try:
cursor.execute(sql)
return cursor.fetchone()[0]
finally:
cursor.close()
 
M

Martin Doering

Personally, I'd call it "cursor", not "c". Also my DB predeliction
leads me to prefer calling it a "row", not a "line".

Yes, for shure. It was my first try. :)

If you know (and don't care to check) that a single row is returned,
with a single value, I might try something like this:

def single_value_query(db, sql):
cursor = db.cursor()
try:
cursor.execute(sql)
return cursor.fetchone()[0]
finally:
cursor.close()

Ah! This is exactly, what I was searching for. For shure, I must force
the query to just deliver ONE value back.

Thanks also to Heather. The indexing was the bit I did not know till
now.

The more I do with [J|Y]ython, the more I like the language. Very
elegant and clear to read. :)
 
D

Dang Griffith

Hi!


I want to use Jython to fetch exactly one value from a database query,
like here:

# execute a query
def query(db, sql):
c = db.cursor()
c.execute(sql)
for line in c.fetchall():
val = line
print val
c.close()
return val


I could find out, how to create a tuple with just one member, but I
can not find out how to get a value from a tuple with just one member.

You might check your database manual. You might be able to have the
query return only one row. E.g., in Oracle, you can add
"and rownum=1" to the query and you'll only get back one row. Since it
looks like you're opening, fetching and closing the cursor all in one
function, and you said you only want one row, you should save some
disk and cpu and bandwidth cycles and make your query more precise.
--dang
 
P

Peter Otten

Martin said:
I could find out, how to create a tuple with just one member, but I
can not find out how to get a value from a tuple with just one member.
tpl = "first item",
tpl ('first item',)
tpl[0]
'first item'

You already got the above solution. Here is another one:

The trailing comma looks strange, but works on both sides of '='.
Packing/unpacking of tuples is remarkably flexible:
'3b'

It works even with function arguments:
.... print a, b, c
....
Peter
 
M

Martin Doering

You already got the above solution. Here is another one:

'first item'

Cool. And I don't find it too ugly. You just see the ',' and know,
that it is a sequence. Sound's logical, even if it is just one item.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top