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

16 浏览
0 Comments

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

在使用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],但没有成功。

0