Page 1 of 1

Accessing an SQL database

Posted: Mon Apr 20, 2020 9:29 pm
by andrew
From a QCAD user:
How can I access an SQL database from my script?

Re: Accessing an SQL database

Posted: Mon Apr 20, 2020 9:37 pm
by andrew
The QCAD script interface provides access to databased through the Qt SQL module (QSqlDatabase, QSqlQuery, etc.).

This example show how to create a new database using the included SQLite driver:

Code: Select all

var db = QSqlDatabase.addDatabase("QSQLITE", "MyConnection");
db.setDatabaseName("mysql.sql");
db.open();

db.transaction();
var query = new QSqlQuery(db);
var ret = query.exec("CREATE TABLE IF NOT EXISTS MyTable (id INTEGER)");
db.commit();
For more information about the Qt SQL module, please refer to the Qt web site at:
https://doc.qt.io/qt-5/qtsql-module.html