Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of field values from Python's sqlite3, not tuples representing rows

Tags:

python

sqlite

It's annoying how Python's sqlite3 module always returns a list of tuples! When I am querying a single column, I would prefer to get a plain list.

e.g. when I execute

SELECT somecol FROM sometable 

and call

cursor.fetchall() 

it returns

[(u'one',), (u'two',), (u'three',)] 

but I'd rather just get

[u'one', u'two', u'three'] 

Is there a way to do this?

like image 605
Jeremiah Rose Avatar asked May 18 '10 01:05

Jeremiah Rose


People also ask

How fetch all data from SQLite database in Python?

SQLite Python: Querying Data First, establish a connection to the SQLite database by creating a Connection object. Next, create a Cursor object using the cursor method of the Connection object. Then, execute a SELECT statement. After that, call the fetchall() method of the cursor object to fetch the data.

How do I get a list of column names in SQLite?

We can also execute the SQLite command that returns all the information of the SQlite database. To get the name of the columns from a sqlite database table we can use the standard Sql query PRAGMA table_info(table_name) . This is the Sql statement that returns all the information of the sqlite database.

How do I see all columns in SQLite?

Show all columns in a SQLite table. In TablePlus, you can either open the Query Editor and run the statements above, or see all columns from the GUI's table structure view: From the table data view, to switch to structure view, click on the Structure button at the bottom of the window, or press Command + Control + ].

How do I get a list of tables in SQLite database in Python?

To list all tables in an SQLite3 database, you should query the sqlite_master table and then use the fetchall() to fetch the results from the SELECT statement. The sqlite_master is the master table in SQLite3, which stores all tables.


2 Answers

sqlite3.Connection has a row_factory attribute.

The documentation states that:

You can change this attribute to a callable that accepts the cursor and the original row as a tuple and will return the real result row. This way, you can implement more advanced ways of returning results, such as returning an object that can also access columns by name.

To return a list of single values from a SELECT, such as an id, you can assign a lambda to row_factory which returns the first indexed value in each row; e.g:

import sqlite3 as db  conn = db.connect('my.db') conn.row_factory = lambda cursor, row: row[0] c = conn.cursor() ids = c.execute('SELECT id FROM users').fetchall() 

This yields something like:

[1, 2, 3, 4, 5, 6] # etc. 

You can also set the row_factory directly on the cursor object itself. Indeed, if you do not set the row_factory on the connection before you create the cursor, you must set the row_factory on the cursor:

c = conn.cursor() c.row_factory = lambda cursor, row: {'foo': row[0]} 

You may redefine the row_factory at any point during the lifetime of the cursor object, and you can unset the row factory with None to return default tuple-based results:

c.row_factory = None c.execute('SELECT id FROM users').fetchall() # [(1,), (2,), (3,)] etc. 
like image 163
Darragh Enright Avatar answered Sep 21 '22 13:09

Darragh Enright


data=cursor.fetchall() COLUMN = 0 column=[elt[COLUMN] for elt in data] 

(My previous suggestion, column=zip(*data)[COLUMN], raises an IndexError if data is an empty tuple. In contrast, the list comprehension above just creates an empty list. Depending on your situation, raising an IndexError may be preferable, but I'll leave that to you to decide.)

like image 20
unutbu Avatar answered Sep 23 '22 13:09

unutbu