Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get extra commas in my python slqlite SELECT results?

I can't figure out what I am doing wrong. Can someone please help? When I run the following statement:

cur.execute("SELECT created FROM datafiles where path = '%s'" % self.srchfilepath.displayText()
list = cur.fetchall()

(self.srchfilepath.displayText() is just a field on a GUI)

The results I get is:

[('testing',), ('testing',)]

The actual data is correct, it is just in a format I don't understand.

I can't figure out where the extra commas within the parens are coming from. I am trying to put this data in back into a listbox field for display and selection, but I need a list that the field will accept. Clearly this is not what it wants. Can anyone tell me what I am doing wrong and how I can make the result into something the Listbox will accept?

like image 574
Gary Kean Avatar asked Dec 07 '22 05:12

Gary Kean


1 Answers

You retrieved a list of rows, and each row is a tuple of columns. As each row contains only one column the rows are tuples with one value each.

In python, it is the comma that makes an expression a tuple, even if there is only one element:

>>> 1,
(1,)

Simply select the first element of each row:

result_list = [row[0] for row in cur.fetchall()]
like image 191
Martijn Pieters Avatar answered Dec 08 '22 19:12

Martijn Pieters