【问题标题】:How to fetch data from postgresql using QLineEdit text() into QtableWidgit如何使用 QLineEdit text() 从 postgresql 获取数据到 QtableWidgit
【发布时间】:2020-08-25 17:39:34
【问题描述】:

我正在尝试使用 if 语句从 postgreSQL 获取数据到 QTableWidget,但是当我应用变量并分配空值(无)时,我的表中没有显示任何内容。我不能将 where 子句与QlineEdit 一起使用。是否有任何可能的方法来重现此代码以使其正常工作?

def LoadData(self):
    name = self.Name_search.text()
    conn = psycopg2.connect(
            database = "postgres",
            user = "postgres",
            password = "**********",
            host = "localhost",
            port = "5432"
            ) 
    if name is None:
    with conn:
        cur = conn.cursor()
        rows = cur.execute("Select * from swimming_pool_users where name = '%s'",(name))
        data = cur.fetchall()
        for row in data:
            self.AddTable(row)
        cur.close()
        
def AddTable(self,columns):
    rowPosition = self.tableWidget2.rowCount()
    self.tableWidget2.insertRow(rowPosition)
    for i, column in enumerate(columns):
        self.tableWidget2.setItem(rowPosition, i, QtWidgets.QTableWidgetItem(str(column)))
def ClearTableData (self):
    while (self.tableWidget2.rowCount() > 0):
        self.tableWidget2.removeRow(0)

【问题讨论】:

    标签: python postgresql pyqt5 qtablewidget qlineedit


    【解决方案1】:

    我真的不明白你到底想要什么,但这是一个如何将数据从 postgresql 数据库显示到 QtableWidgit 的示例

    def exemple_Qtablewidgit(self):
            connection = psycopg2.connect(user="postgres",
                                   password="password",
                                   host="localhost",
                                   database="database")
            self.cur = connection.cursor()
    
            name = self.lineEdit.text()
    
            self.cur.execute(''' SELECT * FROM exemple WHERE name =%s''', (name,))
            data = self.cur.fetchall()
    
            if data :
                self.tableWidget.setRowCount(0)
    
                self.tableWidget.insertRow(0)
                for row, form in enumerate(data):
                    for column , item in enumerate(form):
                        self.tableWidget.setItem(row, column, QTableWidgetItem(str(item)))
                        column += 1 
    
                    row_position  = self.tableWidget.rowCount()
                    self.tableWidget_3.insertRow(row_position)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-19
      • 2012-08-23
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多