Function that returns a tuple
__peter__ at web.de
Sun Jun 17 12:12:03 CEST 2007
> On 17 juin, 11:16, Marcpp <mar... at gmail.com> wrote:
>> On 17 jun, 03:53, Dan Hipschman <d... at linux.ucla.edu> wrote:> On Sat, Jun
>> 16, 2007 at 06:30:26PM -0700, Marcpp wrote:
>> > > Hi, I need to returns a tuple from a function (reads a database)
>> > > Any idea?.
>> > Like this?
>> > def foo():
>> > return 1, 2, 3, 4
>> Hi, I need to return a tupla like this function:
>> def BDllids(a):
>> a = ()
>> conn = sqlite.connect('tasques.db')
>> cursor = conn.cursor()
>> cursor.execute('SELECT * FROM tasques')
>> for row in cursor:
>> a.append (row)
>> return a()
>> I'm doing the correct, method?
> Why is 'a' used as argument of the function ?
> You don't need to put it in argument.
> def BDllids():
a =  # must be a list; tuples don't have an
# append() method
> conn = sqlite.connect('tasques.db')
> cursor = conn.cursor()
> cursor.execute('SELECT * FROM tasques')
Make that "SELECT yadda FROM tasques", assuming that 'yadda' is the first
column in the table.
> for row in cursor:
> a.append (row)
> return a
> But that's the correct method !
Note that the result is now a list, and that is probably the appropriate
data structure; if you needed a tuple you'd have to convert it:
return tuple(a) # easy, obvious, but probably superfluous
More information about the Python-list