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. DB unique instance
Qt 6.11 is out! See what's new in the release blog

DB unique instance

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.4k Views 2 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.
  • ZoptuneZ Offline
    ZoptuneZ Offline
    Zoptune
    wrote on last edited by
    #1

    Hello,

    I don't know how to share the connection between 2 class.
    Should i use the singleton design pattern ?

    Thanks for help

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

      hi
      Qt can share the default connection/db internally.
      So you can open db in one class
      and if you use QSqlQuery in rest of program and if you dont supply
      DB to QSqlQuery , it will use this default one.

      The class holding the DB must of course live on.

      1 Reply Last reply
      1
      • ZoptuneZ Zoptune

        Hello,

        I don't know how to share the connection between 2 class.
        Should i use the singleton design pattern ?

        Thanks for help

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @Zoptune said:

        Should i use the singleton design pattern ?

        No. You shouldn't ever use that unless you're aware of its many, many drawbacks.

        To expand a bit on @mrjj's answer, you can name your connections when creating them with QSqlDatabase::addDatabase, then you can retrieve your connection by its name through QSqlDatabase::database. Something like this:

        // In class 1, main() or w/e
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "mysqlite"); //< Create sqlite connection, name it
        
        // In class 2
        QSqlDatabase db = QSqlDatabase::database("mysqlite"); //< Use the name to retrieve the connection
        

        If you're using only one connection in your program, then you can skip the names (Qt provides a default name), so you can do it simply like:

        // In class 1, main() or w/e
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //< Create sqlite connection, and use the default name
        
        // In class 2
        QSqlDatabase db = QSqlDatabase::database(); //< Get the connection with the default name
        

        Kind regards.

        Read and abide by the Qt Code of Conduct

        ZoptuneZ 1 Reply Last reply
        1
        • kshegunovK kshegunov

          @Zoptune said:

          Should i use the singleton design pattern ?

          No. You shouldn't ever use that unless you're aware of its many, many drawbacks.

          To expand a bit on @mrjj's answer, you can name your connections when creating them with QSqlDatabase::addDatabase, then you can retrieve your connection by its name through QSqlDatabase::database. Something like this:

          // In class 1, main() or w/e
          QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "mysqlite"); //< Create sqlite connection, name it
          
          // In class 2
          QSqlDatabase db = QSqlDatabase::database("mysqlite"); //< Use the name to retrieve the connection
          

          If you're using only one connection in your program, then you can skip the names (Qt provides a default name), so you can do it simply like:

          // In class 1, main() or w/e
          QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //< Create sqlite connection, and use the default name
          
          // In class 2
          QSqlDatabase db = QSqlDatabase::database(); //< Get the connection with the default name
          

          Kind regards.

          ZoptuneZ Offline
          ZoptuneZ Offline
          Zoptune
          wrote on last edited by
          #4

          @kshegunov

          This is the solution i used :)

          1 Reply Last reply
          1

          • Login

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