#includeQSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "myConnection"); db.setDatabaseName("myDb.sqlite"); if (!db.open()) { // handle error }
#includeIn this example, we are creating a connection to a MySQL database and setting its connectionName property to "myConnection". Package library: Qt C++ library (QtSql module)QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "myConnection"); db.setHostName("localhost"); db.setDatabaseName("myDb"); db.setUserName("myUser"); db.setPassword("myPassword"); if (!db.open()) { // handle error }