我有个小问题。
我试图做的是使用一个函数从数据库中获取列名和数据,该函数返回两个值,就像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()
结果进行迭代,并使用列上的循环或格式化字符串打印所需内容相关问题 更多 >
编程相关推荐