【问题标题】:Having an SQLite database how to iterate thru all table items only knowing table name?拥有一个 SQLite 数据库如何只知道表名就遍历所有表项?
【发布时间】:2019-11-05 03:01:12
【问题描述】:

我知道表名,有 SQLLite .db 文件。我不想知道它的模式遍历字典数组(每条记录的一个项目包含对:column name : data)。如何用 sqlalchemy 和 python 做这样的事情?

【问题讨论】:

标签: python json database sqlite sqlalchemy


【解决方案1】:

假设您想在 Python 中执行此操作:

import sqlite3
conn=sqlite3.connect("YourDatabase.db")
c=conn.cursor()
table=list(c.execute("""SELECT * FROM YourTable"""))
columns=list(c.execute("""PRAGMA table_info('YourTable')"""))
recordicts=[]
for record in table:
    dict1={}
    for i in range(len(record)):
        dict1.update({columns[i][1] : record[i]})
    recordicts.append(dict1)
print(recordicts)

希望这会有所帮助! -西奥

【讨论】:

    猜你喜欢
    • 2012-01-07
    • 2017-09-08
    • 2016-08-14
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多