在 Python 中将 SQLite 3 数据存储为变量 [英] Store SQLite 3 Data as a Variable in Python
本文介绍了在 Python 中将 SQLite 3 数据存储为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是否可以将存储在 sqlite3 表中的数据用作 Python 变量?我正在寻找可能与此伪代码类似的东西:
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")
推荐答案
要从表中读取单个值,请使用 SELECT 查询,该查询返回具有单行单列的结果:
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
这篇关于在 Python 中将 SQLite 3 数据存储为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文