繁体   English   中英

如何使用 QLineEdit text() 从 postgresql 获取数据到 QtableWidgit

[英]How to fetch data from postgresql using QLineEdit text() into QtableWidgit

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

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)

我真的不明白你到底想要什么,但这是一个如何将数据从 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM