我想知道如何在 QDateEdit 中获取用户输入并在 postgres 的表中选择它?这是我的代码
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 的表中选择它?
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
![enter image description here](https://i.stack.imgur.com/uyedm.jpg)
你必须:
- 使用 datetime.time() 而不是 QDate。
- 占位符不带引号。
dt = self.dateEdit.date().toPyDate()
cur.execute("SELECT * FROM data WHERE stdate = %s", (dt,))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)