The PySide layer is nearly 1 to 1 for all the Qt methods in C++. Some C++ code is given below.
Because the Qt Sql layer is abstracted for the backend database type, and geared towards Gui interfaces on an event loop, it doesn't have the same one liners available that R or other languages may have. Though you can do it in a few lines.
Also Qt's error handling methods for SQL is usually through querying the last error or looking at the return of the exec
or the open
call. Tuples aren't native in C++, so the python interface doesn't heavily use tuples.
http://doc.qt.io/qt-4.8/sql-sqlstatements.html
http://doc.qt.io/qt-4.8/qsqltablemodel.html#details
QSqlTableModel model;
model.setTable("employee");
model.setFilter("salary > 50000");
model.setSort(2, Qt::DescendingOrder);
model.select();
for (int i = 0; i < model.rowCount(); ++i) {
QString name = model.record(i).value("name").toString();
int salary = model.record(i).value("salary").toInt();
qDebug() << name << salary;
}
Alternate syntax specifying the query
QSqlQueryModel model;
model.setQuery("SELECT * FROM employee");
int salary = model.record(4).value("salary").toInt();
http://doc.qt.io/qt-4.8/qsqlresult.html#size
QSqlQuery query("SELECT country FROM artist");
while (query.next()) {
QString country = query.value(0).toString();
doSomething(country);
}
The real power of Qt's Sql interface is in how easy it is to make a GUI for representing a database in almost any sort of configuration you can think of, and how it is abstracted from the database engine.
Even with all that said about how Qt handles it's SQL calls... it still is on par with other Python libraries for interacting with databases:
How to retrieve SQL result column value using column name in Python?
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT name, category FROM animal")
result_set = cursor.fetchall()
for row in result_set:
print "%s, %s" % (row["name"], row["category"])
Hope that helps.