简体   繁体   中英

Easiest way to display data in PyQt from my Sqlite database in a table?

def showdata(self):
    sender=self.sender()
    self.statusBar().showMessage(sender.text()+' was pressed.')
    with sqlite3.connect('database.db') as db:
        cursor=db.cursor()
        cursor.execute('select* from Item Order BY Name ASC')
        product=cursor.fetchall()
        product=str(product)
        print(len(product))
        self.l.setText(str(product)) #this prints the database headers to a label inside my gui
        self.l124.setText(show) #this prints the database rows to a label inside my gui

This calls to my sqlite database and print all the data out in brackets in one line, such as (2, 'test', 'test', 'test', 'test', 'test', 'test', 'test')], with 2 being the unique id, and the other values input by the user. It also print out the database headers, separately to the rows, I would like to have them together to add simplicity. If any more information is required to aid me with my problem, or any code, please let me know.

You need to think in terms of processing the results one row at a time instead of en masse .

cursor=db.cursor()
cursor.execute('select* from Item Order BY Name ASC')
self.setupHeaderLabels(cursor.description)
for row in cursor:
    self.showOneRow(row)

Then just implement the setupHeaderLabels and showOneRow methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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