我在 python 上撰寫了一個 webapp,在代碼的一部分上我必須從資料庫中獲取一個值。此值在串列中,但此串列的長度為 1。我怎么能把它拿出來而不做一個只會運行一次的for?
順便說一句,這是代碼
if not password and email:
flash("Please enter an email and a password to login", category="error")
else:
con = sqlite3.connect("DB.db")
cur = con.cursor()
cur.execute("SELECT * FROM users WHERE email = ?", (email,))
user = cur.fetchall()
con.close()
if not user:
flash("This account doesn't exist")
else:
for User in user: #<-- this for
pass
uj5u.com熱心網友回復:
如果您知道串列總是有一個元素,您可以輕松訪問第一個元素list[0]
(our_user,) = user[0]
編輯:謝謝@jedwards
uj5u.com熱心網友回復:
如果它只有一個元素,則列印整個串列:
print(*user)
或使用串列中的第一個也是唯一的元素
user[0]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/453657.html
