For the SQLite database, once we open it, it creates the database on the filesystem. For MySQL, we have to send MySQL commands to create the database. We construct the SQL query using QSqlQuery
do this in MySQL. QSqlQuery
takes the database object as an argument:
QSqlQueryquery(db);
To send a query, we call the exec()
function on the QSqlQuery
object. It takes a String
as a typical query
syntax:
QStringdbname="MAEPQTdb"; if (!query.exec("CREATEDATABASEIFNOTEXISTS"+dbname)) { qWarning() << "Database query error" << query.lastError().text(); }
dbname
here is any String
we want the database name to be; I am using MAEPQT db
.
If this command fails, we issue a warning message. If it succeeds, then we go on and issue the command to USE
it, so we call another query
command:
query.exec("USE"+dbname);
From here, we need to create some tables. I will keep it simple and fill it with some data.
We start another query, but with an empty command, and the db
object...