152
db = sqlite.connect("test.sqlite")
res = db.execute("select * from table")

通过迭代,我得到与行相对应的列表。

for row in res:
    print row

我可以得到列的名称

col_name_list = [tuple[0] for tuple in res.description]

但是是否有一些功能或设置来获取字典而不是列表?

{'col1': 'value', 'col2': 'value'}

还是我必须自己做?

4

15 回答 15

219

您可以使用row_factory,如文档中的示例所示:

import sqlite3

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

con = sqlite3.connect(":memory:")
con.row_factory = dict_factory
cur = con.cursor()
cur.execute("select 1 as a")
print cur.fetchone()["a"]

或遵循文档中此示例之后给出的建议:

如果返回元组还不够,并且您希望基于名称访问列,则应考虑将 row_factory 设置为高度优化的 sqlite3.Row 类型。Row 提供对列的基于索引和不区分大小写的基于名称的访问,几乎没有内存开销。它可能比您自己的基于字典的自定义方法甚至基于 db_row 的解决方案更好。

这是第二个解决方案的代码:

con.row_factory = sqlite3.Row
于 2010-07-21T14:47:20.827 回答
57

我想我会回答这个问题,即使 Adam Schmideg 和 Alex Martelli 的回答都部分提到了这个问题。为了让像我这样有同样问题的人能轻松找到答案。

conn = sqlite3.connect(":memory:")

#This is the important part, here we are setting row_factory property of
#connection object to sqlite3.Row(sqlite3.Row is an implementation of
#row_factory)
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('select * from stocks')

result = c.fetchall()
#returns a list of dictionaries, each item in list(each dictionary)
#represents a row of the table
于 2017-01-29T11:04:54.993 回答
23

即使使用 sqlite3.Row 类——你仍然不能使用以下形式的字符串格式:

print "%(id)i - %(name)s: %(value)s" % row

为了解决这个问题,我使用了一个辅助函数来获取行并转换为字典。我只在字典对象比 Row 对象更可取时使用它(例如,对于 Row 对象本身也不支持字典 API 的字符串格式等事情)。但在所有其他时间都使用 Row 对象。

def dict_from_row(row):
    return dict(zip(row.keys(), row))       
于 2012-03-02T18:19:59.670 回答
18

连接到 SQLite 后: con = sqlite3.connect(.....)运行以下命令就足够了:

con.row_factory = sqlite3.Row

瞧!

于 2019-05-04T21:31:34.650 回答
8

来自PEP 249

Question: 

   How can I construct a dictionary out of the tuples returned by
   .fetch*():

Answer:

   There are several existing tools available which provide
   helpers for this task. Most of them use the approach of using
   the column names defined in the cursor attribute .description
   as basis for the keys in the row dictionary.

   Note that the reason for not extending the DB API specification
   to also support dictionary return values for the .fetch*()
   methods is that this approach has several drawbacks:

   * Some databases don't support case-sensitive column names or
     auto-convert them to all lowercase or all uppercase
     characters.

   * Columns in the result set which are generated by the query
     (e.g.  using SQL functions) don't map to table column names
     and databases usually generate names for these columns in a
     very database specific way.

   As a result, accessing the columns through dictionary keys
   varies between databases and makes writing portable code
   impossible.

所以,是的,自己做。

于 2010-07-21T14:46:42.613 回答
4

较短的版本:

db.row_factory = lambda c, r: dict([(col[0], r[idx]) for idx, col in enumerate(c.description)])
于 2017-01-31T11:41:14.197 回答
3

在我的测试中最快:

conn.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r))
c = conn.cursor()

%timeit c.execute('SELECT * FROM table').fetchall()
19.8 µs ± 1.05 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)

与:

conn.row_factory = lambda c, r: dict([(col[0], r[idx]) for idx, col in enumerate(c.description)])
c = conn.cursor()

%timeit c.execute('SELECT * FROM table').fetchall()
19.4 µs ± 75.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

你决定 :)

于 2018-02-14T14:29:36.507 回答
3

类似于前面提到的解决方案,但最紧凑:

db.row_factory = lambda C, R: { c[0]: R[i] for i, c in enumerate(C.description) }
于 2018-04-09T03:55:06.003 回答
2

正如@gandalf 的回答所提到的,一个人必须使用conn.row_factory = sqlite3.Row,但结果不是直接的字典。dict必须在最后一个循环中添加一个额外的“演员” :

import sqlite3
conn = sqlite3.connect(":memory:")
conn.execute('create table t (a text, b text, c text)')
conn.execute('insert into t values ("aaa", "bbb", "ccc")')
conn.execute('insert into t values ("AAA", "BBB", "CCC")')
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('select * from t')
for r in c.fetchall():
    print(dict(r))

# {'a': 'aaa', 'b': 'bbb', 'c': 'ccc'}
# {'a': 'AAA', 'b': 'BBB', 'c': 'CCC'}
于 2019-10-25T23:11:09.333 回答
2

获取查询结果

output_obj = con.execute(query)
results = output_obj.fetchall()

选项 1) 带 Zip 的显式循环

for row in results:
    col_names = [tup[0] for tup in output_obj.description]
    row_values = [i for i in row]
    row_as_dict = dict(zip(col_names,row_values))

选项 2) 更快的循环 w/ Dict Comp

for row in results:
    row_as_dict = {output_obj.description[i][0]:row[i] for i in range(len(row))}
于 2021-03-27T13:46:10.270 回答
1

我认为你在正确的轨道上。让我们保持这个非常简单并完成您想要做的事情:

import sqlite3
db = sqlite3.connect("test.sqlite3")
cur = db.cursor()
res = cur.execute("select * from table").fetchall()
data = dict(zip([c[0] for c in cur.description], res[0]))

print(data)

缺点是.fetchall(),如果您的表非常大,这对您的内存消耗是致命的。但是对于仅处理几千行文本和数字列的琐碎应用程序,这种简单的方法就足够了。

对于严肃的事情,您应该研究行工厂,正如许多其他答案中所建议的那样。

于 2019-12-29T21:36:31.753 回答
0

或者您可以将 sqlite3.Rows 转换为字典,如下所示。这将为字典提供每行的列表。

    def from_sqlite_Row_to_dict(list_with_rows):
    ''' Turn a list with sqlite3.Row objects into a dictionary'''
    d ={} # the dictionary to be filled with the row data and to be returned

    for i, row in enumerate(list_with_rows): # iterate throw the sqlite3.Row objects            
        l = [] # for each Row use a separate list
        for col in range(0, len(row)): # copy over the row date (ie. column data) to a list
            l.append(row[col])
        d[i] = l # add the list to the dictionary   
    return d
于 2014-08-21T21:04:29.177 回答
0

一个通用的替代方案,只使用三行

def select_column_and_value(db, sql, parameters=()):
    execute = db.execute(sql, parameters)
    fetch = execute.fetchone()
    return {k[0]: v for k, v in list(zip(execute.description, fetch))}

con = sqlite3.connect('/mydatabase.db')
c = con.cursor()
print(select_column_and_value(c, 'SELECT * FROM things WHERE id=?', (id,)))

但是,如果您的查询没有返回任何内容,则会导致错误。在这种情况下...

def select_column_and_value(self, sql, parameters=()):
    execute = self.execute(sql, parameters)
    fetch = execute.fetchone()

    if fetch is None:
        return {k[0]: None for k in execute.description}

    return {k[0]: v for k, v in list(zip(execute.description, fetch))}

或者

def select_column_and_value(self, sql, parameters=()):
    execute = self.execute(sql, parameters)
    fetch = execute.fetchone()

    if fetch is None:
        return {}

    return {k[0]: v for k, v in list(zip(execute.description, fetch))}
于 2015-10-13T17:16:50.410 回答
0
import sqlite3

db = sqlite3.connect('mydatabase.db')
cursor = db.execute('SELECT * FROM students ORDER BY CREATE_AT')
studentList = cursor.fetchall()

columnNames = list(map(lambda x: x[0], cursor.description)) #students table column names list
studentsAssoc = {} #Assoc format is dictionary similarly


#THIS IS ASSOC PROCESS
for lineNumber, student in enumerate(studentList):
    studentsAssoc[lineNumber] = {}

    for columnNumber, value in enumerate(student):
        studentsAssoc[lineNumber][columnNames[columnNumber]] = value


print(studentsAssoc)

结果肯定是真的,但我不知道最好的。

于 2017-12-27T13:59:07.763 回答
0

python 中的字典提供对其元素的任意访问。因此,任何带有“名称”的字典虽然一方面可能提供信息(即字段名称是什么),但会“取消排序”字段,这可能是不需要的。

最好的方法是在单独的列表中获取名称,然后在需要时自行将它们与结果结合起来。

try:
         mycursor = self.memconn.cursor()
         mycursor.execute('''SELECT * FROM maintbl;''')
         #first get the names, because they will be lost after retrieval of rows
         names = list(map(lambda x: x[0], mycursor.description))
         manyrows = mycursor.fetchall()

         return manyrows, names

还要记住,在所有方法中,名称都是您在查询中提供的名称,而不是数据库中的名称。例外是SELECT * FROM

如果您唯一关心的是使用字典获取结果,那么一定要使用conn.row_factory = sqlite3.Row(已经在另一个答案中说明)。

于 2018-12-24T08:04:15.443 回答