How it works…

The previous example shows you how to connect to your SQL database using the QSqlDatabase class derived from the SQL module. You won't be able to access any of the classes related to SQL without adding the module to your Qt project.

We must tell Qt which SQL architecture we are running by mentioning it when calling the addDatabase() function. Options supported by Qt are QSQLITE, QMYSQL, QMYSQL3, QODBC, QODBC3, QPSQL, and QPSQL7. If you encounter an error message that says QSqlDatabase: QMYSQL driver not loaded, you should check whether the DLL files are placed in the correct directory.

We can send our SQL statements to the database through the QSqlQuery class, and wait for it to return the results, which are usually either the data you requested or error messages due to invalid statements. If there is any data coming from the database server, it will all be stored in the QSqlQuery class. All you need to retrieve this data is to do a while loop on the QSqlQuery class to check for all existing records, and retrieve them by calling the value() function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset