gpt4 book ai didi

python - 如何从 python 中的 SQL 查询中获取单个结果?

转载 作者:IT老高 更新时间:2023-10-28 22:10:51 24 4
gpt4 key购买 nike

在使用 Python 时,是否有一种优雅的方法可以从 SQLite SELECT 查询中获取单个结果?

例如:

conn = sqlite3.connect('db_path.db')
cursor=conn.cursor()
cursor.execute("SELECT MAX(value) FROM table")

for row in cursor:
for elem in row:
maxVal = elem

有没有办法避免那些嵌套的 for 直接获取值?我试过了

maxVal = cursor[0][0]

没有任何成功。

最佳答案

我想你正在寻找 Cursor.fetchone() :

cursor.fetchone()[0]

关于python - 如何从 python 中的 SQL 查询中获取单个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com