使用Python3和SQLlite存储字典数据

时间:2018-08-18 04:42:09

标签: python sqlite

这是我的代码:

document.querySelector("#suario-select")

这是我的数据:

document.querySelector("p#usuario-select")

这就是我想要的字典

def getDownloaders(dbPATH):
 with sqlite3.connect(dbPATH) as db:
      cursor = db.cursor()
      cursor.execute("SELECT * FROM Downloaders")
      d = cursor.fetchall()

      downloader = {}
      column_names = [s[0] for s in cursor.description]

      for i in range(len(d)):
        for row in d:
            downloader[i] = dict(zip(column_names, row))

 print(downloader)
 return downloader  

1 个答案:

答案 0 :(得分:1)

对于所有行索引和所有行,您都有两个嵌套的for循环,因此最里面的行可以看到irow(3×3)的所有组合,甚至这两个不匹配的人。

您必须使用一个循环:

cursor.execute("...")
column_names = [s[0] for s in cursor.description]
downloader = {}
i = 0
for row in cursor:
  downloader[i] = dict(zip(column_names, row))
  i += 1

以连续数字作为键的字典毫无意义;使用数组作为返回值会更简单:

cursor.execute("...")
column_names = [s[0] for s in cursor.description]
downloader = [dict(zip(column_names, row)) for row in cursor]