P
P
pypyshka2017-06-07 12:57:49
Python
pypyshka, 2017-06-07 12:57:49

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)

Loading a table of several thousand rows is, in principle, fast. But I had to make some cells in the table editable. Therefore, I decided to use the QStandardItemModel 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)

But this method takes a lot of time to load data into the table (as I understand it, because of the for loops). Is there any way to load the data into the table as quickly as possible, while leaving the possibility of editing? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pypyshka, 2017-06-08
@pypyshka

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 question

Ask a Question

731 491 924 answers to any question