Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8315129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:03:37+00:00 2026-06-08T21:03:37+00:00

I want to insert my data which is stored in a sqlite table, to

  • 0

I want to insert my data which is stored in a sqlite table, to a QTableWidget. I use two for loop to find the data and index. after each iteration I print the data in console and it is OK but when it displays the table widget there is only the first row and the last row filled with the data.
Any idea to solve this problem?

It’s obvious that tblTable is a QTableWidget!

Here is this part of the code:

cursor.execute('''SELECT * FROM MyTable''')
for index , form in enumerate(cursor.fetchall()):
    i = 0
    for item in form:
        print(str(item))
        self.tblTable.setItem(index, i, QtGui.QTableWidgetItem(str(item)))       
        i = i + 1
    self.tblTable.insertRow(1)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:03:39+00:00Added an answer on June 8, 2026 at 9:03 pm

    You keep inserting your new row at position 1. What happens is that the previously entered data is then moved up one row, at which point you overwrite that data in the next loop.

    So, first iteration everything is inserted in row 0, you add a row at index 1. Then you update row 1 with data, and insert another row at position 1, making the previously modified row move to row 2. Next loop, you overwrite the data on row 2, insert another empty row at position 1, moving the row with data to position 3 and you overwrite it again, etc., etc.

    Set the row-count to 0 at the start, and insert rows as you need them before you insert your column data:

    cursor.execute('''SELECT * FROM MyTable''')
    self.tblTable.setRowCount(0)
    for row, form in enumerate(cursor):
        self.tblTable.insertRow(row)
        for column, item in enumerate(form):
            print(str(item))
            self.tblTable.setItem(row, column, QtGui.QTableWidgetItem(str(item)))       
    

    I am not that familiar with the QtTableWidget, it could be that continually adding rows in not going to perform as well as setting the number of rows up front.

    If sqlite’s cursor.rowcount attribute is properly updated on your query (it not always is), you’d be better off calling .setRowCount with that value:

    cursor.execute('''SELECT * FROM MyTable''')
    self.tblTable.setRowCount(cursor.rowcount)
    for row, form in enumerate(cursor):
        for column, item in enumerate(form):
            self.tblTable.setItem(row, column, QtGui.QTableWidgetItem(str(item)))       
    

    If the .rowcount value is not available (set to 1 or similar), perhaps first asking the database for the number of rows can help:

    rowcount = cursor.execute('''SELECT COUNT(*) FROM MyTable''').fetchone()[0]
    self.tblTable.setRowCount(rowcount)
    cursor.execute('''SELECT * FROM MyTable''')
    for row, form in enumerate(cursor):
        for column, item in enumerate(form):
            self.tblTable.setItem(row, column, QtGui.QTableWidgetItem(str(item)))       
    

    In all examples above, I also renamed you variables to something a little closer to their use, and used enumerate on the item loop as well. Last, but not least, the cursor can act as an iterator, meaning you can loop over rows directly without calling .fetchall() and rows will be fetched as needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to return a temporary table from stored procedure which populates with data
I want to create a service which will insert provided data into spreadsheet fields
I want to insert data into a table where I don't know the next
I want to call the Stored Procedure which returns a table in the Table
I have a table:'Categories' which has two fields:Category_ID and Category. Data in Category_ID field
I want to secure events stored in one table, which has relations to others.
I'm trying to insert data into a table using a plpgsql function or stored
I'm using a stored procedure to update\insert data into a table using MERGE. One
I have old table which the data stored as tbl_id tbl_data -------------------- 1 1,2,3,4,5,6
I need a data structure in which each element has a specific index but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.