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

how to get user input from qdateEdit and select it from database in postgres(如何从 qdateEdit 获取用户输入并从 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 的数据库中选择它

基础教程推荐