這個問題在這里已經有了答案: Sqlite LIMIT / OFFSET 查詢 3 個答案 昨天關門。
我是 Python 新手,最近一直堅持從 Sqlite 獲取價值。我想要的是只在表格的第二行獲得價值。這是表格資料圖片。我已經嘗試過了,但沒有作業:
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)
這將帶來第一行和第二行的值。但我想要的只是第二行值。有人幫忙嗎?
uj5u.com熱心網友回復:
fetchall 回傳對串列的參考。您的查詢將最多回傳 2 行。所以:
if (row := cur.fetchall()):
print(row[-1])
else:
print('Not found')
這樣做允許結果為空或僅包含一行
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/442093.html
