Store SQLite 3 Data as a Variable in Python

2019-03-05 04:58发布

Is it possible for me to take data stored in a sqlite3 table and use it as a Python variable? I'm looking for something that might be similar to this pseudo-code:

import sqlite3
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
variable = cursor.execute("fetch data from table")

1条回答
霸刀☆藐视天下
2楼-- · 2019-03-05 05:20

To read a single value from a table, use a SELECT query that returns a result with a single row and a single column:

for row in cursor.execute("SELECT MyColumn FROM MyTable WHERE ID = ?", [123]):
    variable = row[0]
    break
else:
    variable = 0  # not found
查看更多
登录 后发表回答