Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why can't QSqlDatabase be a dynamic variable?

Why can't QSqlDatabase be a dynamic variable?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 414 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Seb TurS Offline
    Seb TurS Offline
    Seb Tur
    wrote on last edited by Seb Tur
    #1

    I have my connection working via odbc in Linux using

    db_static=QSqlDatabase::addDatabase("QODBC","db");
    

    but it fails when I put :

    db_ptr=new QSqlDatabase();
    db_ptr->addDatabase("QODBC","db");
    

    All other db settings are set by the same methods just changing . to ->

    Is it by design that static should be used?

    J.HilkJ 1 Reply Last reply
    0
    • Seb TurS Seb Tur

      I have my connection working via odbc in Linux using

      db_static=QSqlDatabase::addDatabase("QODBC","db");
      

      but it fails when I put :

      db_ptr=new QSqlDatabase();
      db_ptr->addDatabase("QODBC","db");
      

      All other db settings are set by the same methods just changing . to ->

      Is it by design that static should be used?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Seb-Tur
      define it fails


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi

        Actually it compiles fine here

        auto db_ptr=new QSqlDatabase();
        db_ptr->addDatabase("QODBC","db");

        Anyway

        docs says
        "Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. "

        So you dont need to store a pointer to it.

        it has access to it already via QSqlDatabase::database (static call)

        setQuery(SelectStr, QSqlDatabase::database(LOGDBSTR));

        so in 99.999% of all cases you do not need to keep a pointer to it :)

        1 Reply Last reply
        4
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          QSqlDatabase is a singleton that manages all the database connections. It's all explained in the documentation of the class.

          Beside that, addDatabase is a static method returning a QSqlDatabase instance so what you are doing has in fact no effect and you are manipulating an invalid QSqlDatabase object.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved