Last active
December 10, 2015 16:48
-
-
Save lamprosg/4463119 to your computer and use it in GitHub Desktop.
Qt Databases (MySQL)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Create a default connection | |
| QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); //Or whatever database in use | |
| /*We could add a second argument which is the connection name** | |
| Example: | |
| QSqlDatabase firstDB = QSqlDatabase::addDatabase("QMYSQL", "first"); | |
| */ | |
| //Set connection information | |
| db.setHostName("xxx.xxx.xxx.xxx"); //localhost if it's local | |
| db.setDatabaseName("flightdb"); | |
| db.setUserName("acarlson"); | |
| db.setPassword("1uTbSbAs"); | |
| //Open it for use | |
| if ( !db.open() ) | |
| QString error = db.lastError().text(); //Returns databaseText() and driverText() concatenated into a single string | |
| /*Once a connection is established, we can call the static function QSqlDatabase::database() | |
| from anywhere with a connection name to get a pointer to that database connection. | |
| If we don't pass a connection name, it will return the default connection.*/ | |
| QSqlDatabase defaultDB = QSqlDatabase::database(); | |
| //OR | |
| //QSqlDatabase firstDB = QSqlDatabase::database("first"); | |
| //Closing the connection | |
| db.close(); //Close the database | |
| db.removeDatabase() //Remove it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /****http://doc.qt.digia.com/4.7/sql-sqlstatements.html****/ | |
| //The QSqlQuery constructor accepts an optional QSqlDatabase object that specifies which database connection to use. | |
| //If we don't specify any connection, so the default connection is used. | |
| QSqlQuery query; | |
| query.exec("SELECT name, salary FROM employee WHERE salary > 50000"); | |
| //If an error occurs, exec() returns false. The error is then available as QSqlQuery::lastError(). | |
| //Navigate through the result set | |
| // QSqlQuery's internal pointer is located one position before the first record. | |
| //We must call QSqlQuery::next() once to advance to the first record | |
| //then next() again repeatedly to access the other records, until it returns false. | |
| while (query.next()) | |
| { | |
| //QVariant QSqlQuery::value ( int index ) Returns the value of field index in the current record | |
| QString name = query.value(0).toString(); //1st result column (index 1) | |
| int salary = query.value(1).toInt(); //2d result column (index 2) | |
| } | |
| /*-----------------------------------------------------------------------*/ | |
| //How many results do we have | |
| QSqlQuery query; | |
| int numRows; | |
| query.exec("SELECT name, salary FROM employee WHERE salary > 50000"); | |
| QSqlDatabase defaultDB = QSqlDatabase::database(); | |
| if (defaultDB.driver()->hasFeature(QSqlDriver::QuerySize)) { | |
| numRows = query.size(); | |
| } | |
| else | |
| { | |
| // this can be very slow | |
| query.last(); | |
| numRows = query.at() + 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| QT += core gui | |
| QT += sql |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create MySQL driver for Qt on Windows
http://www.seppemagiels.com/blog/create-mysql-driver-qt-windows