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. getting already open database connection
Forum Updated to NodeBB v4.3 + New Features

getting already open database connection

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 761 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by user4592357
    #1

    i have a table view which needs to display info from database. i'm using QSqlDatabase. this is how i open a db connection:

    auto db = QSqlDatabase::database();
    db.setDatabaseName(m_sDatabasePath);
    if (!db.open())
    	error(arg(db.lastError().text());
    

    later i use queries like this one to retrieve the info:

    QSqlQuery sqlQuery(QSqlDatabase::database());
    if (sqlQuery.exec(...) && sqlQuery.first())
    	...
    

    since i'm using the default connection, when i open a new database i cannot use these queries to retrieve info from old connection. i know that i should name the connections and access them via name, but...
    use scenario is the following:

    1. open db #1, issue queries, display data in view (table tab)
    2. open db #2, issue queries, display data in new table tab
    3. switch back to old tab, problem: queries are still executed on db #2 (default connection).

    so i changed connection opening to this:

    auto db = QSqlDatabase::addDatabase("QSQLITE", sDatabasePath);
    db.setDatabaseName(sDatabasePath);
    if (!db.open())
    	error(arg(db.lastError().text());
    

    and changed the queries to this:

    QSqlQuery sqlQuery(QSqlDatabase::database(m_info->getDatabasePath()));
    

    now, when i execute a query, i get Parameter count mismatch error, and i'm not sure why, because before this change everything worked correctly (for default connection).

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Don't think this has something to do with the other database connection. I would take a look at the query where it fails, and check what QSqlQuery::error() and database() really returns.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      U 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        Don't think this has something to do with the other database connection. I would take a look at the query where it fails, and check what QSqlQuery::error() and database() really returns.

        U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

        @Christian-Ehrlicher
        the error is from QSqlQuery::lastError().
        and it worked with default connection

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Please show the query, make sure that the db is really open at the time you're trying to execute the query.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

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

            Hi,

            To add to @Christian-Ehrlicher, I am not sure that using a file path is necessarily a good idea for a connection name.

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

            U 1 Reply Last reply
            1
            • SGaistS SGaist

              Hi,

              To add to @Christian-Ehrlicher, I am not sure that using a file path is necessarily a good idea for a connection name.

              U Offline
              U Offline
              user4592357
              wrote on last edited by
              #6

              @SGaist
              then what should i be using?

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

                A meaningful identifier, just the name of the database, etc. That's up to you.

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

                U 1 Reply Last reply
                0
                • SGaistS SGaist

                  A meaningful identifier, just the name of the database, etc. That's up to you.

                  U Offline
                  U Offline
                  user4592357
                  wrote on last edited by
                  #8

                  @SGaist
                  yeah something unique, so i thought file path would be more unique then fine name

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

                    In any case, you should do as @Christian-Ehrlicher wrote: get the error from the failed query.

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

                    U 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      In any case, you should do as @Christian-Ehrlicher wrote: get the error from the failed query.

                      U Offline
                      U Offline
                      user4592357
                      wrote on last edited by user4592357
                      #10

                      @SGaist
                      the parameter mismatch error is the text given by lastError()

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

                        And what is the query ? The one returned by lastQuery ?

                        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
                        0
                        • U Offline
                          U Offline
                          user4592357
                          wrote on last edited by
                          #12

                          actually prepare() was failing and i got the error from sql query at right after prepare() and it was meaningful

                          1 Reply Last reply
                          0

                          • Login

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