问题
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")
i am having trouble with this part
cur.execute("SELECT * FROM data WHERE stdate = '%s'",date)
how do i get the date from QDateEdit and select it in the table in 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
回答1:
You have to:
- Use a datetime.time() not QDate.
- The placeholder is without quotes.
dt = self.dateEdit.date().toPyDate()
cur.execute("SELECT * FROM data WHERE stdate = %s", (dt,))
来源:https://stackoverflow.com/questions/65207752/how-to-get-user-input-from-qdateedit-and-select-it-from-database-in-postgres