Answer the question
In order to leave comments, you need to log in
How to load data from sqlite database into QTableView as fast as possible?
Good afternoon.
There is a need to upload large amounts of data to the table, for this I do this:
db_connect = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db_connect.setDatabaseName("database.db")
db_connect.open()
model = QtSql.QSqlQueryModel(parent=window)
model.setQuery("SELECT id, num, data FROM table")
window.tableView.setModel(model)
db_connect = sqlite3.connect("database.db")
cur = db_connect.cursor()
cur.execute("SELECT id, num, data FROM table")
content = cur.fetchall()
row_count = len(content)
db_connect.commit()
cur.close()
db_connect.close()
model = QtGui.QStandardItemModel()
lst1 = []
lst2 = []
lst3 = []
for i in content:
lst1.append(i[0])
lst2.append(i[1])
lst3.append(i[2])
for i in range(0, row_count):
item1 = QtGui.QStandardItem(lst1[i])
item2 = QtGui.QStandardItem(lst2[i])
item3 = QtGui.QStandardItem(lst3[i])
model.appendRow([item1, item2, item3])
item1.setEditable(False)
window.tableView.setModel(model)
Answer the question
In order to leave comments, you need to log in
In general, I tried to create a model based on QAbstractTableModel. I don’t know how exactly in terms of speed, but the data is visually loaded into the tableView faster than when using QStandardItemModel. And now it has become easier to work with the data and with the table itself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question