[英]Query data from database on sql condition in python
just a small problem I have.只是我遇到的一个小问题。 What I am trying to do is get both column names and data from a database, using a function which returns two values just like
return columns, data
.我想做的是使用 function 从数据库中获取列名和数据,它返回两个值,就像
return columns, data
一样。 The problem that I have is outputting the data.我遇到的问题是输出数据。 Using the following code
使用以下代码
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)
the print is印刷品是
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')
but I want it to be something like但我希望它像
Name: Andrei
Surname: Cioban
# and so on
Edit: This is the sqlite3 code that queries the data:编辑:这是查询数据的 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
How can I do this?我怎样才能做到这一点? What I've tried doesn't work.
我尝试过的方法不起作用。 I don't understand why it prints a tuple after I looped through the tuple return with the data from the database.
我不明白为什么在我使用数据库中的数据循环返回元组后它会打印一个元组。
fetchall()
gets you a list of the rows in your database. fetchall()
为您获取数据库中的行列表。
So the comprehension list will just contain every tuple returned by fecthall()
(meaning it is useless and you could just have written for detail in fetchall()
.所以理解列表将只包含
fecthall()
返回的每个元组(这意味着它是无用的,您可以for detail in fetchall()
。
As a result, detail
will take values from a list of tuple, so in each loop iteration, detail
will be a row of your database.因此,
detail
将从元组列表中获取值,因此在每次循环迭代中, detail
将是数据库的一行。
Maybe you could iterate over the fetchall()
result with for row in fetchall
and print what you want using a loop on the columns or a formated string.也许您可以使用
for row in fetchall
遍历fetchall()
结果,并使用列上的循环或格式化字符串打印您想要的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.