O'Reilly logo

PySide GUI Application Development - Second Edition by Venkateshwaran Loganathan, Gopinath Jaganmohan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required