May 2019
Intermediate to advanced
542 pages
13h 37m
English
Interacting with our SQL database relies on the QSqlQuery class. This class represents a request to the SQL engine and can be used to prepare, execute, and retrieve data and metadata about a query.
We can make a SQL query to the database by using our database object's exec() method:
query = self.db.exec('SELECT count(*) FROM coffees')
The exec() method creates a QSqlQuery object from our string, executes it, and returns it to us. We can then retrieve the results of our query from the query object:
query.next() count = query.value(0) print(f'There are {count} coffees in the database.')
It's important to get a mental model of what's happening here, because it's not terribly intuitive. As you know, SQL queries always ...
Read now
Unlock full access