Sebastien
Sebastien

Reputation: 2679

Generate single-dimensional array with MySQL query

I have a simple 2 column table with the fields id and entry.

I want to select only the entries:

SELECT entry FROM table

However this query generates a multi dimensional array. I need to generate a simple, single-dimensional array. I know I could possibly do this after in my Phyhon code but the database is very large (100,000+ columns) and I'd like to not have to go through this extra step before I can exploit my data.

Upvotes: 1

Views: 139

Answers (1)

falsetru
falsetru

Reputation: 369134

Unpack the result as follow:

cursor.execute('SELECT entry FROM table')
rows = [entry for entry, in cursor.fetchall()]
#                      ^

>>> rows = [('entry1',), ('entry2',), ('entry3',)]
>>> rows
[('entry1',), ('entry2',), ('entry3',)]
>>> rows = [entry for entry, in rows]
>>> rows
['entry1', 'entry2', 'entry3']

Upvotes: 1

Related Questions