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
. 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:
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.
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()
.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.