0
    with sqlite3.connect('database.db') as db:
        cursor=db.cursor()
        cursor.execute('select* from Item Order BY Name ASC')
        title = [cn[0] for cn in cursor.description]
        rows = [cn[0] for cn in cursor.description]
        cur=cursor.fetchall()


        layout = QtGui.QGridLayout() 
        self.test = QtGui.QLineEdit('test') #This is just a test for printing text into the table and works fine.

        self.table = QtGui.QTableWidget()

        for rows in cur:
            print(rows[1]) #This is just a test to see if the data could be printed into python shell, which worked.


            #self.test2 = QtGui.QLineEdit(cur)
            #self.table.setVerticalHeaderLabels(rows)

        self.table.setRowCount(3)
        self.table.setColumnCount(5)
        self.table.setHorizontalHeaderLabels(title) #This code works fine, the column headers are the ones from my database.
        #self.table.setVerticalHeaderItem(1,row)

        layout.addWidget(self.table, 0, 0)
        self.table.setItem(0, 0, QtGui.QTableWidgetItem(self.test.text()))
        #self.table.setItem(1, 0, QtGui.QTableWidgetItem(self.test2.text()))

        self.setLayout(layout)

Hashed out code are areas I played with but are causing errors, or just not working. I attached all of the code for the table to make it easier to understand what I am trying to accomplish.

This program currently opens a table with the column headers from the database, with 3 empty rows (excluding test). I would like the fill these rows automatically with the value from my sqlite database. If there is any information you need to know please ask.

1 Answer 1

1

You can do something like this:

    cur=cursor.fetchall()   
    for i,row in enumerate(cur):
        for j,val in enumerate(row):
            table.setItem(i, j, QtGui.QTableWidgetItem(str(val)))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! This worked perfectly, is there also a way I could have the rowcount update itself for each line of data, instead of me setting a row count of 100 etc. ?

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.