Python Sqlite3 Returning query results as a dictionary method introduction

Source: Internet
Author: User
The sqlite3 itself does not provide a cursor in the form of a dictionary as native as Pymysql.

cursor = conn.cursor (pymysql.cursors.DictCursor)

But the official document has been reserved for the corresponding implementation plan.

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

Use this function instead of the Conn.raw_factory attribute.

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_factorycur = Con.cursor () Cur.execute ("Select 1 as a") print Cur.fetchone () ["a"]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.