Results 1 to 20 of 61

Thread: using an isntance of QSqlDatabase for connection defiinition

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2016
    Posts
    99
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default using an isntance of QSqlDatabase for connection defiinition

    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-----------
    Qt Code:
    1. void myClass::insertLogMessage(QString msg)
    2. {
    3. //---Contects to a database | Uses QSQLITE driver---//
    4. QSqlDatabase userEventDB = QSqlDatabase::addDatabase("QSQLITE");
    5. //---Sepcifies database path---//
    6. userEventDB.setDatabaseName("/home/amet/git/rnd/userLog.db");
    7. //---Checks if database is open---//
    8. if (userEventDB.open()) {
    9. qDebug() << "Connected to userEventLog database";
    10. }
    11. else {
    12. qDebug() << "Error: no connection was found!";
    13. }
    14. //---Opens database---//
    15. userEventDB.open();
    16.  
    17. qDebug() << "insert Log Message called";
    18. QSqlQuery query;
    19. query.prepare("INSERT INTO userlogevents (firstName, lastName, userName, eventMessage, dateTime) VALUES('John', 'Doe', 'JohnnyD', '"+msg+"', datetime(current_timestamp))");
    20. //---Executes Query Statement---//
    21. query.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 



    ------using instance (QSqlDatabase) variable-----------
    Qt Code:
    1. void myClass::insertLogMessage(QString msg)
    2. {
    3. m_insertDataBase = QSqlDatabase::addDatabase("QSQLITE", "conn1");
    4. m_insertDataBase = QSqlDatabase::database("conn1");
    5. m_insertDataBase.setDatabaseName("/home/amet/userLog.db");
    6. m_insertDataBase.open();
    7.  
    8. if(m_insertDataBase.isOpen()){
    9.  
    10. qDebug() <<"connected to DB" ;
    11. }
    12. else{
    13. qDebug() <<"error in opening DB";
    14. m_insertDataBase.open();
    15. }
    16.  
    17. qDebug() << "insert Log Message called";
    18. QSqlQuery insertQuery("INSERT INTO userlogevents (firstName, lastName, userName, eventMessage, dateTime) VALUES('John', 'Doe', 'JohnnyD', '"+msg+"', datetime(current_timestamp))", conn1);
    19. insertQuery.exec();
    20. m_insertDataBase.close();
    21. }
    To copy to clipboard, switch view to plain text mode 


    --------Header file with instance of QSqlDatabase object---------
    Qt Code:
    1. private:
    2. QSqlDatabase m_insertDataBase;
    3. };
    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?
    Last edited by jfinn88; 23rd August 2016 at 22:14.

Similar Threads

  1. Replies: 16
    Last Post: 4th September 2013, 00:49
  2. How to set x509 on a QSqlDatabase Connection?
    By m3rlin in forum Qt Programming
    Replies: 24
    Last Post: 21st February 2012, 04:04
  3. Windows OCI QSqlDatabase connection
    By hollyberry in forum Newbie
    Replies: 10
    Last Post: 13th February 2012, 22:13
  4. QSqlDatabase Connection Close on Destruction
    By Sanuden in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2011, 15:32
  5. QSqlDatabase connection timeout?
    By joseprl89 in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2011, 01:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.