-1

This project is in Python and will have an sqlite3 database.

I would like to open the sqlite3 database using Python's sqlite3 module. But I also want to have functionality from the PySide6.QtSql module.

PySide's documentation shows an example of this happening with Postgres.

con = PQconnectdb("host=server user=bart password=simpson dbname=springfield")
drv = QPSQLDriver(con)
db = QSqlDatabase.addDatabase(drv) # becomes the default() connection()
query = QSqlQuery()
query.exec("SELECT NAME, ID FROM STAFF")

I tried using QSQLiteDriver, but I cannot import it, and I cannot find the required file qsql_sqlite.cpp on my computer.

The documentation mentions linking certain files. Is it possible to use QSQLiteDriver without compiling PySide6 from source?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
UndoingTech
  • 709
  • 3
  • 16
  • 2
    As most PySide docs, what you're reading is the copy of the original C++ API documentation (in fact, the notes about headers and application linking make absolutely no sense in Python). Besides, why do you want to use the same connection object? And what functionalities do you need from the `sqlite3` module that the QtSqlDatabase module doesn't provide? – musicamante Oct 18 '22 at 23:11
  • @musicamante I use `sqlite3` `register_adapter` and `register_converter` to automatically convert decimals and strings. I would very much like to use `decimal` with `QtSql`. – UndoingTech Oct 19 '22 at 00:19
  • I also will need to [define aggregate functions](https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_aggregate). – UndoingTech Oct 19 '22 at 00:40
  • 1
    This doesn't really answer the question about the need for using just a single connection. Note that you can always subclass QSqlTableModel (or QSqlQueryModel, if you're using that) and provide different results for both the `DisplayRole` and `EditRole`. You can even consider using a QIdentityProxyModel, or a specific QStyledItemDelegate. – musicamante Oct 19 '22 at 00:49
  • 2
    Those driver classes are only available in C++. There's no way for PySide or PyQt to wrap them because the drivers are implemented as plugins. There's also no way to directly register any kind of extension, adapter, aggregator etc using the Qt classes via Python. It can only be done via C++. If you want to use Python's sqlite implementation with Qt, you will have to write your own custom model (i.e. based on QAbstractItemModel), which should give you something equivalent to QSqlQueryModel (but obviously not based on QSqlQuery). The other Qt sql classes aren't worth bothering with. – ekhumoro Oct 19 '22 at 11:34

1 Answers1

2

Those driver classes are only available in C++. There's no way for PySide or PyQt to wrap them because the drivers are implemented as plugins. There's also no way to directly register any kind of extension, adapter, aggregator etc using the Qt classes via Python. It can only be done via C++.

Python's SQLite implementation is generally much more flexible than Qt's - the only really important thing it cannot offer is a model. For that, you have two main possiblilities: use a QStandardItemModel and populate it directly from the query results; or write a custom model (i.e. based on QAbstractItemModel, with fetchMore), which should give you something equivalent to QSqlQueryModel (but obviously not based on QSqlQuery). The main reason for choosing one over the other is performance. A QStandardItemModel will work fine for smallish result sets, but the overhead of creating a QStandardItem for every row may start to become noticeable above ten thousand items or so (depending on the number of columns, local system resources, etc).

The other low-level Qt SQL classes probably aren't worth bothering with, since Python already offers similar functionality in most cases. If you have a reasonable knowledge of Python's sqlite implementation it should be possible integrate it fairly easily with a Qt GUI using a just basic model.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336