Set class for database connection
-
HI I am a beginner in Gui development using qt.
I have a project where I have to use a mysql database. I want to know how to create a class for my connection to the database and for the query so It will be easy for me to call this class in the different windows and execute sql queries -
Hi,
Did you take a look at the QtSql module documentation and examples ?
-
Yeah I did. I created an header file called connection for the connection to the database as follows
@@
#include <QtSql>
#include <QSql>static bool connOpen()
{QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("calendar");
db.setUserName("root");
db.setPassword("");
if(!db.open())
{
qDebug()<<("Failed to open the database");
return false;
}
else
{qDebug()<<("Connected"); return true;
}
}
@@
I am able to add in my database but when I trying to display in a dialog windows using a table view, nothings shows in my table view -
How are you setting up your model ?