[英]Query data from database on sql condition in python
只是我遇到的一個小問題。 我想做的是使用 function 從數據庫中獲取列名和數據,它返回兩個值,就像return columns, data
一樣。 我遇到的問題是輸出數據。 使用以下代碼
for column in build_sql.show_employee_id(registered_employee[0])[1]:
for detail in [b for b in build_sql.show_employee_id(registered_employee[0])[0].fetchall()]:
print(column, ":", detail)
印刷品是
Name : ('Andrei', 'Cioban', '09:28:32 (19.12.2020)', 352395729385723, 'wfoaF@fwaic.com', 'AWiofOAWFLAWiufnLIAWUfnliaIUFAWi', 'Employee', '411372')
Surname : ('Andrei', 'Cioban', '09:28:32 (19.12.2020)', 352395729385723, 'wfoaF@fwaic.com', 'AWiofOAWFLAWiufnLIAWUfnliaIUFAWi', 'Employee', '411372')
HiredDate : ('Andrei', 'Cioban', '09:28:32 (19.12.2020)', 352395729385723, 'wfoaF@fwaic.com', 'AWiofOAWFLAWiufnLIAWUfnliaIUFAWi', 'Employee', '411372')
PhoneNumber : ('Andrei', 'Cioban', '09:28:32 (19.12.2020)', 352395729385723, 'wfoaF@fwaic.com', 'AWiofOAWFLAWiufnLIAWUfnliaIUFAWi', 'Employee', '411372')
Email : ('Andrei', 'Cioban', '09:28:32 (19.12.2020)', 352395729385723, 'wfoaF@fwaic.com', 'AWiofOAWFLAWiufnLIAWUfnliaIUFAWi', 'Employee', '411372')
Address : ('Andrei', 'Cioban', '09:28:32 (19.12.2020)', 352395729385723, 'wfoaF@fwaic.com', 'AWiofOAWFLAWiufnLIAWUfnliaIUFAWi', 'Employee', '411372')
Position : ('Andrei', 'Cioban', '09:28:32 (19.12.2020)', 352395729385723, 'wfoaF@fwaic.com', 'AWiofOAWFLAWiufnLIAWUfnliaIUFAWi', 'Employee', '411372')
BadgeID : ('Andrei', 'Cioban', '09:28:32 (19.12.2020)', 352395729385723, 'wfoaF@fwaic.com', 'AWiofOAWFLAWiufnLIAWUfnliaIUFAWi', 'Employee', '411372')
但我希望它像
Name: Andrei
Surname: Cioban
# and so on
編輯:這是查詢數據的 sqlite3 代碼:
def show_employee_id(ide):
employee = connection.execute('SELECT Name, Surname, HiredDate, PhoneNumber, Email, Address, Position, BadgeID FROM employees WHERE ID = ?;', (ide,))
columns = [description[0] for description in employee.description]
return employee, columns
我怎樣才能做到這一點? 我嘗試過的方法不起作用。 我不明白為什么在我使用數據庫中的數據循環返回元組后它會打印一個元組。
fetchall()
為您獲取數據庫中的行列表。
所以理解列表將只包含fecthall()
返回的每個元組(這意味着它是無用的,您可以for detail in fetchall()
。
因此, detail
將從元組列表中獲取值,因此在每次循環迭代中, detail
將是數據庫的一行。
也許您可以使用for row in fetchall
遍歷fetchall()
結果,並使用列上的循環或格式化字符串打印您想要的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.