Fetching a variable

67 Views Asked by At

I got this code:

cursor.execute('SELECT nom FROM productes WHERE listacompra = 1')
producteslc = cursor.fetchone()

The problem is that when I do print producteslc, it returns (u'Natillas',), when the value on the SQL Database is just Natillas.

What could I do to have a variable with value = Natillas? I'm trying to do some stuff with split but I'm not able to do it at my own.

Thank you

1

There are 1 best solutions below

7
On BEST ANSWER

The result of fetchone is a tuple of the values of one row.

Since you only fetch a single column, the result is a tuple singleton: (u'Natillas',)

To get the string:

producteslc = cursor.fetchone()[0]

See: Tuples and Sequences in the doc

EDIT

To fetch several rows, you can use fetchall() function:

rows = cursor.fetchall()
for row in rows:
    print(row[0])

To print each name.