这是一个自包含的示例,说明了总体思路。 numpy.recarray是你的朋友,
from sqlite3 import connect
from numpy import asarray
db = connect(":memory:")
c = db.cursor()
c.execute('create table bigtop (a int, b int, c int)')
for v in [(1,2,3),(4,5,6),(7,8,9)]:
c.execute('insert into bigtop values (?,?,?)',v)
s = c.execute('select * from bigtop')
h = [(i[0],int) for i in c.description]
# You can also use 'object' for your type
# h = [(i[0],object) for i in c.description]
a = asarray(list(s),dtype=h)
print a['a']
给出第一列,
[1 4 7]
和,
print a.dtype
给出每列的名称和类型,
[('a', '<i4'), ('b', '<i4'), ('c', '<i4')]
或者,如果你使用 object 作为你的类型,你会得到,
[('a', '|O4'), ('b', '|O4'), ('c', '|O4')]