how to get user input from qdateEdit and select it from database in postgres

夙愿已清 提交于 2021-02-17 04:00:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!