<legend id='2cQFC'><style id='2cQFC'><dir id='2cQFC'><q id='2cQFC'></q></dir></style></legend>
    <bdo id='2cQFC'></bdo><ul id='2cQFC'></ul>

  • <small id='2cQFC'></small><noframes id='2cQFC'>

    1. <i id='2cQFC'><tr id='2cQFC'><dt id='2cQFC'><q id='2cQFC'><span id='2cQFC'><b id='2cQFC'><form id='2cQFC'><ins id='2cQFC'></ins><ul id='2cQFC'></ul><sub id='2cQFC'></sub></form><legend id='2cQFC'></legend><bdo id='2cQFC'><pre id='2cQFC'><center id='2cQFC'></center></pre></bdo></b><th id='2cQFC'></th></span></q></dt></tr></i><div id='2cQFC'><tfoot id='2cQFC'></tfoot><dl id='2cQFC'><fieldset id='2cQFC'></fieldset></dl></div>
      <tfoot id='2cQFC'></tfoot>

      1. 如何从sqlite查询中获取dict?

        时间:2023-10-10

          <tbody id='GoAMQ'></tbody>
        <legend id='GoAMQ'><style id='GoAMQ'><dir id='GoAMQ'><q id='GoAMQ'></q></dir></style></legend>

          <bdo id='GoAMQ'></bdo><ul id='GoAMQ'></ul>

        • <i id='GoAMQ'><tr id='GoAMQ'><dt id='GoAMQ'><q id='GoAMQ'><span id='GoAMQ'><b id='GoAMQ'><form id='GoAMQ'><ins id='GoAMQ'></ins><ul id='GoAMQ'></ul><sub id='GoAMQ'></sub></form><legend id='GoAMQ'></legend><bdo id='GoAMQ'><pre id='GoAMQ'><center id='GoAMQ'></center></pre></bdo></b><th id='GoAMQ'></th></span></q></dt></tr></i><div id='GoAMQ'><tfoot id='GoAMQ'></tfoot><dl id='GoAMQ'><fieldset id='GoAMQ'></fieldset></dl></div>
            1. <tfoot id='GoAMQ'></tfoot>

                <small id='GoAMQ'></small><noframes id='GoAMQ'>

                  本文介绍了如何从sqlite查询中获取dict?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  With iteration I get lists coresponding to the rows.

                  for row in res:
                      print row
                  

                  我可以得到列的名称

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

                  但是是否有一些函数或设置可以获取字典而不是列表?

                  But is there some function or setting to get dictionaries instead of list?

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

                  还是我必须自己做?

                  推荐答案

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

                  You could use row_factory, as in the example in the docs:

                  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"]
                  

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

                  or follow the advice that's given right after this example in the docs:

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

                  If returning a tuple doesn’t suffice and you want name-based access to columns, you should consider setting row_factory to the highly-optimized sqlite3.Row type. Row provides both index-based and case-insensitive name-based access to columns with almost no memory overhead. It will probably be better than your own custom dictionary-based approach or even a db_row based solution.

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

                  Here is the code for this second solution:

                  con.row_factory = sqlite3.Row
                  

                  这篇关于如何从sqlite查询中获取dict?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:多列上的Sqlite主键 下一篇:SQLite 表约束 - 在多列上唯一

                  相关文章

                  <small id='12cWG'></small><noframes id='12cWG'>

                        <bdo id='12cWG'></bdo><ul id='12cWG'></ul>
                      <i id='12cWG'><tr id='12cWG'><dt id='12cWG'><q id='12cWG'><span id='12cWG'><b id='12cWG'><form id='12cWG'><ins id='12cWG'></ins><ul id='12cWG'></ul><sub id='12cWG'></sub></form><legend id='12cWG'></legend><bdo id='12cWG'><pre id='12cWG'><center id='12cWG'></center></pre></bdo></b><th id='12cWG'></th></span></q></dt></tr></i><div id='12cWG'><tfoot id='12cWG'></tfoot><dl id='12cWG'><fieldset id='12cWG'></fieldset></dl></div>
                    1. <tfoot id='12cWG'></tfoot>
                      <legend id='12cWG'><style id='12cWG'><dir id='12cWG'><q id='12cWG'></q></dir></style></legend>