0

I'm trying to execute a SQL statement in Python/pyqt5, but every time I run this code

self.view.resizeColumnToContents()

I get an error:

TypeError: resizeColumnToContents(self, int): not enough arguments

I've been trying to use different SQL statements, but no success

import sys
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
from PyQt5.QtWidgets import QApplication, QMessageBox, QLabel, QMainWindow, QTableWidget, QTableWidgetItem

class mainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle(" work overflow")
        self.resize(1000, 500)
        #view setup
        self.view = QTableWidget()
        #2 kolumny
        self.view.setColumnCount(2)
        self.view.setHorizontalHeaderLabels(["EWR", "Description", " id "])
        query = QSqlQuery("SELECT * from _tbl_BuildPhases")
        while query.next():
            rows = self.view.rowCount()
            self.view.setRowCount(rows+1)
            self.view.setItem(rows, 0, QTableWidgetItem(query.value(0)))
            #self.view.setItem(rows, 1, QTableWidgetItem(str(query.value(1))))
            self.view.setItem(rows, 1, QTableWidgetItem(query.value(1)))

        self.view.resizeColumnToContents()
        self.setCentralWidget(self.view)



#Creating connection with DB
def createConnection():
    con = QSqlDatabase.addDatabase("QSQLITE")
    con.setDatabaseName("xyz")
    con.setUserName("sa")
    con.setPassword("xyz")

    if not con.open():
        QMessageBox.critical(None, "work overflow- Error",
                             "DataBase Error: %s" % con.lastError().databaseText())
        return False
    return True


# create App
app = QApplication(sys.argv)
#App window
if not createConnection():
    sys.exit(1)

win = mainWindow()
win.show()
sys.exit(app.exec_())
7
  • 1
    That error message has absolutely nothing to do with SQL or your data operation .... Commented Mar 7, 2021 at 9:53
  • resizeColumnToContents takes an argument, which column to resize. Have a look at the docs Commented Mar 7, 2021 at 10:00
  • Ok, passing argument into resizeColumnToContents helped but data from sql is missing Commented Mar 7, 2021 at 11:40
  • @Spinny Why do you think the data is "missing"? Have you actually checked that the sql statement is correct, and, if so, how? Commented Mar 7, 2021 at 15:41
  • @ekhumoro Because in mainWindow where i display ewr - description-id there is no data only column names. Sql statment is correct base on data from DB - where i have 26 records Commented Mar 7, 2021 at 17:28

1 Answer 1

1

solved

import sys
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
from PyQt5.QtWidgets import QApplication, QMessageBox, QLabel, QMainWindow, QTableWidget, QTableWidgetItem


class mainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("ProtoLab work overflow")
        self.resize(1000, 500)
        #view setup
        self.view = QTableWidget()
        #2 kolumny
        self.view.setColumnCount(3)
        self.view.setHorizontalHeaderLabels(["EWR", "Description", " x "])
        query = QSqlQuery("SELECT * from [EWR_ProtoLab].[dbo].[_tbl_BuildPhases]")
        while query.next():
            rows = self.view.rowCount()
            self.view.setRowCount(rows+1)
            self.view.setItem(rows, 0, QTableWidgetItem(str(query.value(0))))
            self.view.setItem(rows, 1, QTableWidgetItem(str(query.value(1))))
            self.view.setItem(rows, 2, QTableWidgetItem(query.value(2)))

        #self.view.resizeColumnToContents(1)
        self.setCentralWidget(self.view)



#Creating connection with DB
def createConnection():
 
    constring = ('DRIVER={ODBC Driver 11 for SQL Server};''SERVER=xyz;DATABASE=xxx;UID=sa;PWD=xyz')
    con = QSqlDatabase.addDatabase("QODBC")

    con.setDatabaseName(constring)

    if not con.open():
        QMessageBox.critical(None, "ProtoLAB work overflow- Error",
                             "DataBase Error: %s" % con.lastError().databaseText())
        return False
    return True


# create App
app = QApplication(sys.argv)
#App window
if not createConnection():
    sys.exit(1)

win = mainWindow()
win.show()
sys.exit(app.exec_())

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.