用户登录
用户注册

分享至

如何从 qdateEdit 获取用户输入并从 postgres 的数据库中选择它

  • 作者: 5555558497484
  • 来源: 51数据库
  • 2022-12-23

问题描述

我想知道如何在 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="*****", 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,))
软件
前端设计
程序设计
Java相关