如何只从 Sqlite 获取第二行数据?
How to only get second row data from Sqlite?
我是 Python 的新手,我最近坚持从 Sqlite 获取价值。我想要的是仅在 table 的第二行中获取值。这是tabledata picture。我试过了但没有用:
con = sqlite3.connect(database=r'ims.db')
cur = con.cursor()
for l in range(1,8):
cur.execute(f"Select occolor{l} from ordercart limit 2")
row = cur.fetchall()
print(row)
这将带来第一行和第二行的值。但我想要的只是第二行的值。有人帮忙吗?
fetchall returns 对列表的引用。您的查询最多 return 2 行。因此:
if (row := cur.fetchall()):
print(row[-1])
else:
print('Not found')
这样做允许结果为空或仅包含一行
我是 Python 的新手,我最近坚持从 Sqlite 获取价值。我想要的是仅在 table 的第二行中获取值。这是tabledata picture。我试过了但没有用:
con = sqlite3.connect(database=r'ims.db')
cur = con.cursor()
for l in range(1,8):
cur.execute(f"Select occolor{l} from ordercart limit 2")
row = cur.fetchall()
print(row)
这将带来第一行和第二行的值。但我想要的只是第二行的值。有人帮忙吗?
fetchall returns 对列表的引用。您的查询最多 return 2 行。因此:
if (row := cur.fetchall()):
print(row[-1])
else:
print('Not found')
这样做允许结果为空或仅包含一行