I have a Sqlite database I need to connect to...
Instead of running the connection code every-time my method is called I would like to create an instance of the QSqlDatabase class
I'm new to QT and still learning C++
my understanding the code will run more efficiently and will "clean up" the code as well and make it so only create a connection once unless needed
if you can explain the how the instance of the class will allow me to do this that would be awesome
----------old way-----------
void myClass
::insertLogMessage(QString msg
) {
//---Contects to a database | Uses QSQLITE driver---//
//---Sepcifies database path---//
userEventDB.setDatabaseName("/home/amet/git/rnd/userLog.db");
//---Checks if database is open---//
if (userEventDB.open()) {
qDebug() << "Connected to userEventLog database";
}
else {
qDebug() << "Error: no connection was found!";
}
//---Opens database---//
userEventDB.open();
qDebug() << "insert Log Message called";
query.prepare("INSERT INTO userlogevents (firstName, lastName, userName, eventMessage, dateTime) VALUES('John', 'Doe', 'JohnnyD', '"+msg+"', datetime(current_timestamp))");
//---Executes Query Statement---//
query.exec();
}
void myClass::insertLogMessage(QString msg)
{
//---Contects to a database | Uses QSQLITE driver---//
QSqlDatabase userEventDB = QSqlDatabase::addDatabase("QSQLITE");
//---Sepcifies database path---//
userEventDB.setDatabaseName("/home/amet/git/rnd/userLog.db");
//---Checks if database is open---//
if (userEventDB.open()) {
qDebug() << "Connected to userEventLog database";
}
else {
qDebug() << "Error: no connection was found!";
}
//---Opens database---//
userEventDB.open();
qDebug() << "insert Log Message called";
QSqlQuery query;
query.prepare("INSERT INTO userlogevents (firstName, lastName, userName, eventMessage, dateTime) VALUES('John', 'Doe', 'JohnnyD', '"+msg+"', datetime(current_timestamp))");
//---Executes Query Statement---//
query.exec();
}
To copy to clipboard, switch view to plain text mode
------using instance (QSqlDatabase) variable-----------
void myClass
::insertLogMessage(QString msg
) {
m_insertDataBase
= QSqlDatabase::addDatabase("QSQLITE",
"conn1");
m_insertDataBase.setDatabaseName("/home/amet/userLog.db");
m_insertDataBase.open();
if(m_insertDataBase.isOpen()){
qDebug() <<"connected to DB" ;
}
else{
qDebug() <<"error in opening DB";
m_insertDataBase.open();
}
qDebug() << "insert Log Message called";
QSqlQuery insertQuery
("INSERT INTO userlogevents (firstName, lastName, userName, eventMessage, dateTime) VALUES('John', 'Doe', 'JohnnyD', '"+msg
+"', datetime(current_timestamp))", conn1
);
insertQuery.exec();
m_insertDataBase.close();
}
void myClass::insertLogMessage(QString msg)
{
m_insertDataBase = QSqlDatabase::addDatabase("QSQLITE", "conn1");
m_insertDataBase = QSqlDatabase::database("conn1");
m_insertDataBase.setDatabaseName("/home/amet/userLog.db");
m_insertDataBase.open();
if(m_insertDataBase.isOpen()){
qDebug() <<"connected to DB" ;
}
else{
qDebug() <<"error in opening DB";
m_insertDataBase.open();
}
qDebug() << "insert Log Message called";
QSqlQuery insertQuery("INSERT INTO userlogevents (firstName, lastName, userName, eventMessage, dateTime) VALUES('John', 'Doe', 'JohnnyD', '"+msg+"', datetime(current_timestamp))", conn1);
insertQuery.exec();
m_insertDataBase.close();
}
To copy to clipboard, switch view to plain text mode
--------Header file with instance of QSqlDatabase object---------
private:
};
private:
QSqlDatabase m_insertDataBase;
};
To copy to clipboard, switch view to plain text mode
I'm running into issues trying to implement an instance of the QSqlDatabase class (to connect to database just once) When I call my query with my database named connection ("conn1") I get 'conn1' was not declared in this scope I need to create an instance of this class (QSqlDatabase) to connect/open database once...? can any one help me get a better understanding of instances and creating a connection to a database with an instance of QSqlDatabase class so you only have to connect to it once or if you need to connect to it agian you just use the instance variable?
Bookmarks