0
我有一個簡單的Qt應用程序,它使用自定義小部件添加並列出從QListView中的postgresql db中檢索到的記錄。我有一些困難,建立一個按鈕(每個列表項實例的左下角),這將有效地使用記錄的主鍵(實例),以從DB將方法連接到自定義QListView小部件 - PyQt5&postgreSQL
from PyQt5 import QtCore, QtWidgets
import psycopg2
import sys
import dbAPP_GUI
import listItem_GUI
class dbAPP(QtWidgets.QMainWindow, dbAPP_GUI.Ui_MainWindow):
def __init__(self, parent=None):
super(dbAPP, self).__init__(parent)
self.setupUi(self)
self.conn = psycopg2.connect(database="Name", user="[email protected]"
, password="14581458", host="127.0.0.1")
self.ListItemLine = listItem()
# ==============
# CONNECTIONS
# ==============
self.btn_save.clicked.connect(self.writeData)
self.tabWidget.currentChanged.connect(self.fetchData)
self.btn_go.clicked.connect(self.searchData)
# =============
# METHODS
# =============
def writeData(self):
name = self.name_lineEdit.text()
surn = self.surname_lineEdit.text()
age = self.age_lineEdit.text()
address = self.address_lineEdit.text()
if len(name) > 0:
c = self.conn.cursor()
c.execute(" INSERT INTO info (name, surname, age, address)"
"VALUES (%s, %s, %s, %s)", (name, surn, age, address))
self.conn.commit()
QtWidgets.QMessageBox.information(self, "Notice", "Information was successfully written to database")
self.name_lineEdit.clear()
self.surname_lineEdit.clear()
self.age_lineEdit.clear()
self.address_lineEdit.clear()
else:
QtWidgets.QMessageBox.warning(self, "Warning", "The name field is empty, please enter a value")
def fetchData(self):
self.listWidget.clear()
lineNo = 0
c = self.conn.cursor()
c.execute("SELECT * FROM info;")
listing = c.fetchall()
for i in listing:
name = (str(i[:1]).replace("(", "").replace(")", "").replace("'", "").replace(",", ""))
surn = (str(i[1:2]).replace("(", "").replace(")", "").replace("'", "").replace(",", ""))
age = (str(i[2:3]).replace("(", "").replace(")", "").replace("'", "").replace(",", ""))
address = (str(i[3:4]).replace("(", "").replace(")", "").replace("'", "").replace(",", ""))
ID = (str(i[4:5]).replace("(", "").replace(")", "").replace("'", "").replace(",", ""))
nameSurn = name + " " + surn
lineNo += 1
label = listItem()
label.num_label.setText(str(lineNo))
label.nameSurn_label.setText(nameSurn)
label.age_label.setText(age)
label.address_label.setText(address)
label.id_label.setText(ID)
x = QtWidgets.QListWidgetItem()
x.setSizeHint(QtCore.QSize(280, 60))
self.listWidget.addItem(x)
self.listWidget.setItemWidget(x, label)
label.show()
def searchData(self):
self.listWidget_2.clear()
lineNo = 0
c = self.conn.cursor()
c.execute("SELECT * FROM info;")
results = c.fetchall()
for i in results:
name = (str(i[:1]).replace("(", "").replace(")", "").replace("'", "").replace(",", ""))
surn = (str(i[1:2]).replace("(", "").replace(")", "").replace("'", "").replace(",", ""))
age = (str(i[2:3]).replace("(", "").replace(")", "").replace("'", "").replace(",", ""))
address = (str(i[3:4]).replace("(", "").replace(")", "").replace("'", "").replace(",", ""))
nameSurn = name + " " + surn
if str(self.search_lineEdit.text()).lower() == name.lower()\
or str(self.search_lineEdit.text()).lower() == surn.lower()\
or str(self.search_lineEdit.text()).lower() == address.lower():
lineNo += 1
label = listItem()
label.num_label.setText(str(lineNo))
label.nameSurn_label.setText(nameSurn)
label.age_label.setText(age)
label.address_label.setText(address)
x = QtWidgets.QListWidgetItem()
x.setSizeHint(QtCore.QSize(280, 60))
self.listWidget_2.addItem(x)
self.listWidget_2.setItemWidget(x, label)
label.show()
# ======================================================================================================================
class listItem(QtWidgets.QWidget, listItem_GUI.Ui_lineItem):
def __init__(self, parent=None):
super(listItem, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setupUi(self)
self.btn_del.clicked.connect(self.delData)
def delData(self):
ID = self.id_label.text()
print(ID)
c = psycopg2._ext.connection.cursor()
c.execute("DELETE FROM info WHERE id=(%s)", (ID))
# ======================================================================================================================
app = QtWidgets.QApplication(sys.argv)
app.setStyle("fusion")
main = dbAPP()
main.show()
List = listItem()
List.hide()
sys.exit(app.exec_())
目前它的失敗,因爲刪除的記錄方法「DelData」中的遊標對象需要參數。但是,如果這是做這件事的正確方法,我可以誠實地不說。所以如果有人有更好的建議,那也可以。我只是試圖用PyQt,postgreSQL和Python找到我的腳:)
好吧....我做了微小的變化,謝謝對於輸入:) 但我仍然收到關於查找參數的遊標對象錯誤 – Ants
@Ants 。什麼錯誤?編輯你的問題並添加完整的回溯。 – ekhumoro