简体   繁体   English

从数据库中查询 python 中 sql 条件的数据

[英]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.

相关问题 从 Python 查询 Cloud SQL 数据库 - Query Cloud SQL Database from Python 在Python中从数据库查询创建数据框 - Creating Data Frame From Database Query in Python 从 python dataframe 将数据导入 sql 数据库 - Import data from python dataframe to sql database 如何从 Python 中的 SQL 查询中获取数据 - How to fetch data from SQL query in Python 来自 SQL 查询的 Python 数据分析 - Python Data Analysis from SQL Query SQL / Python:将数据从csv转换为具有条件的不同架构的表 - SQL/Python: Transform data from csv and into table with different schema with condition 在Python中使用IF条件从SQL表中导入数据(或者在Python中只在条件满足时才从SQL表中导入数据) - Use IF condition in Python to Import Data from SQL Table (or Import Data from SQL table only when condition satisfies in Python) 使用Python SqlAlchemy或Sql Query进行数据库数据完整性检查 - Database data Integrity check using Python SqlAlchemy or Sql Query Python:使用SQL查询和嵌套的for循环读取数据并将数据重新插入数据库吗? - Python: Using an SQL query and a nested for loop to read and reinsert data into a database? 使用 Python 对 sql 数据库执行查询 - Perform a query on a sql database with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM