Executing SQL queries
After successful connection to the database, we can execute SQL queries to perform some actions on it. If we don't specify a connection name, the default connection is taken. The PySide.QtSql.QSqlQuery
class provides a means of executing and manipulating SQL databases.
Executing a query
The SQL query can be executed by creating a QSqlQuery
object and calling an exec_()
function on this. As an example, we create a table named employee
and define its columns, as follows:
myQuery = QSqlQuery() myQuery.exec_("""CREATE TABLE employee (id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, first_name CHAR(20) NOT NULL, last_name CHAR(20), age INT, sex CHAR(1), income FLOAT)""")
This will create a table with six columns, namely id, first_name, last_name, age, sex, and income. The QSqlQuery
constructor accepts an optional parameter, a QSqlDatabase
object that specifies which database connection to use. As we don't specify any connection...