how to get user input from qdateEdit and select it from database in postgres(如何从 qdateEdit 获取用户输入并从 postgres 的数据库中选择它)
问题描述
我想知道如何在 QDateEdit 中获取用户输入并在 postgres 的表格中选择它?这是我的代码
i want to know how to get the user input in QDateEdit and select it in a table in postgres? here is my code
def date(self):
try:
date = self.dateEdit.date()
print(date)
conn = psycopg2.connect(dbname="sample", user="postgres", password="admin", host="localhost", port="5432")
cur = conn.cursor()
cur.execute("SELECT * FROM data WHERE stdate = '%s'",date)
result = cur.fetchall()
self.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.tableWidget.setItem(row_number, column_number, QTableWidgetItem(str(data)))
except Exception as e:
print("Error")
这部分有问题
cur.execute("SELECT * FROM data WHERE stdate = '%s'",date)
如何从 QDateEdit 获取日期并在 postgres 的表格中选择它?
how do i get the date from QDateEdit and select it in the table in postgres?
我只想选择标准日期等于用户在 QDateEdit 中输入的日期的行,并在我单击选择数据按钮时将其显示在 QtableView 中
and i only want to select the rows where the stdate is equal to the date that the user input in the QDateEdit and display it in the QtableView when i click the select data button
推荐答案
你必须:
- 使用 datetime.time() 而不是 QDate.
- 占位符不带引号.
dt = self.dateEdit.date().toPyDate()
cur.execute("SELECT * FROM data WHERE stdate = %s", (dt,))
这篇关于如何从 qdateEdit 获取用户输入并从 postgres 的数据库中选择它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 qdateEdit 获取用户输入并从 postgres 的数据库中选择它


- 计算测试数量的Python单元测试 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01