本文介紹了如何從 qdateEdit 獲取用戶輸入并從 postgres 的數(shù)據(jù)庫中選擇它的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我想知道如何在 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?
我只想選擇標(biāo)準(zhǔn)日期等于用戶在 QDateEdit 中輸入的日期的行,并在我單擊選擇數(shù)據(jù)按鈕時(shí)將其顯示在 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,))
這篇關(guān)于如何從 qdateEdit 獲取用戶輸入并從 postgres 的數(shù)據(jù)庫中選擇它的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!