An example using SQL Alchemy:
First create a SQLite DB:
C:\Temp>sqlite3 c:\temp\a.db SQLite version 3.13.0 2016-05-18 10:57:30 Enter ".help" for usage hints. sqlite> .headers on sqlite> .mode column sqlite> create table tab(name varchar(50), val int); sqlite> insert into tab values ('A', 1), ('A', 2), ('A', 3), ('B', 2), ('B', 4), ('C', 6); sqlite> select * from tab; name val ---------- ---------- A 1 A 2 A 3 B 2 B 4 C 6 sqlite> .exit
Now use SQLAchemy:
In [101]: from sqlalchemy import create_engine In [102]: engine = create_engine('sqlite:///c:/temp/a.db') In [103]: conn = engine.connect() In [104]: conn.execute('select * from tab').fetchall() Out[104]: [('A', 1), ('A', 2), ('A', 3), ('B', 2), ('B', 4), ('C', 6)]
Strings and integers ( int ) are defined correctly ...
To get a list of strings, instead of a list of tuples:
In [106]: rows = conn.execute('select name from tab').fetchall() In [107]: rows Out[107]: [('A',), ('A',), ('A',), ('B',), ('B',), ('C',)] In [108]: names = [x[0] for x in rows] In [109]: names Out[109]: ['A', 'A', 'A', 'B', 'B', 'C']
or:
In [113]: from operator import itemgetter In [114]: list(map(itemgetter(0), conn.execute('select name from tab').fetchall())) Out[114]: ['A', 'A', 'A', 'B', 'B', 'C']
fetchonereturns the same asfetchall()[0]- andreymal[x[0] for x in c.fetchall()]and, accordingly,c.fetchone()[0], and shoving it inside sqlite3 makes little sense - andreymal