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. QSqlQuery worked on Linux but not Windows - Parameter mismatch
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery worked on Linux but not Windows - Parameter mismatch

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 619 Views 1 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.
  • kesterpmK Offline
    kesterpmK Offline
    kesterpm
    wrote on last edited by
    #1

    The following code worked in Linux. I'm trying to test my solution on windows now. I'm using sqlite. This code get's a parameter mismatch error on the exec() statement.

    QSqlQuery select("select parameter_value from application_settings where parameter_name = ?");
    select.bindValue(0, t_parametername);
    if (select.exec())
    {
        if (select.next())
            return select.value(0).toString();
        else
            return QString();
    }
    else
    {
        QMessageBox::critical(nullptr, QObject::tr("Database Access Failed"), QString("Failed to access a saved setting. You may need to restart Project Notes.\n\nError:\n%1").arg(select.lastError().text()) );
        return QString();
    }
    
    1 Reply Last reply
    0
    • kesterpmK Offline
      kesterpmK Offline
      kesterpm
      wrote on last edited by
      #9

      OK, that helped. I'll need to change to using prepare() everywhere. I am using 5.15.2 on linux as well, but this is a sqlite issue. See my code change below. The error that prepare() caused was "RIGHT and FULL OUTER JOINs are currently not supported." My sqlite database had a view with a RIGHT JOIN. I removed it and everything works now. The join was on a view I wasn't even using here.

      QSqlQuery select;
      if (!select.prepare("select parameter_value from application_settings where parameter_name = ?"))
          qDebug() << "Prepare Failed: " << select.lastError();
      select.bindValue(0, t_parametername);
      
      if (select.exec())
      {
          if (select.next())
              return select.value(0).toString();
          else
              return QString();
      }
      else
      {
          qDebug() << "Last Error: " <<  select.lastError().databaseText() << "\n" << select.lastError().driverText() << "\n" << select.lastError().nativeErrorCode();
      
          QMessageBox::critical(nullptr, QObject::tr("Database Access Failed"), QString("Failed to access a saved setting. You may need to restart Project Notes.\n\nError:\n%1").arg(select.lastError().text()) );
          return QString();
      }
      
      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #2

        Hi and welcome to the forums

        Try with

        QSqlQuery select;
        select.prepare("select parameter_value from application_settings where parameter_name = ?");
        select.bindValue(0, t_parametername);
        if (select.exec()) ...
        

        As seen here
        https://doc.qt.io/qt-6/qsqlquery.html#QSqlQuery-1

        IF constructed with an non empty string, its executed.

        1 Reply Last reply
        0
        • kesterpmK Offline
          kesterpmK Offline
          kesterpm
          wrote on last edited by
          #3

          thanks, I just tried that. It gives the same result.

          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #4

            If you do it the unsafe way:

            QSqlQuery select(QString("select parameter_value from application_settings where parameter_name = '%1'").arg(t_parametername));
            if (select.exec())
            ...
            

            does it still fail?

            1 Reply Last reply
            0
            • kesterpmK Offline
              kesterpmK Offline
              kesterpm
              wrote on last edited by
              #5

              It doesn't. That solution works fine. I've done something similar. I'm wondering if it is something with my build environment. I'm just starting to do work in Windows. I built the bulk of the application in Linux. I'm just starting to port it over to windows.

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

                What Qt version do you use on Linux and Windows?
                Also are you sure you opened the sqlite database from the correct location?

                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
                0
                • kesterpmK Offline
                  kesterpmK Offline
                  kesterpm
                  wrote on last edited by
                  #7

                  I'm using Qt 5.15.2. I'm sure I have the correct database because using the exec() method works. Everywhere in my code where the bind method is used it fails. I'm compiling with MSVC 16.11.33130.400 (amd64).

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

                    Please use QSqlQuery::prepare() instead the QSqlQuery ctor and check the return value of prepare(). I'm not sure if QSqlQuery(QString) is meant to be used with bound values since as the documentation states, the given query is executed directly which will fail and then the query will be in an invalid state.
                    Are you sure you have 5.15.2 also on Linux?

                    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
                    2
                    • kesterpmK Offline
                      kesterpmK Offline
                      kesterpm
                      wrote on last edited by
                      #9

                      OK, that helped. I'll need to change to using prepare() everywhere. I am using 5.15.2 on linux as well, but this is a sqlite issue. See my code change below. The error that prepare() caused was "RIGHT and FULL OUTER JOINs are currently not supported." My sqlite database had a view with a RIGHT JOIN. I removed it and everything works now. The join was on a view I wasn't even using here.

                      QSqlQuery select;
                      if (!select.prepare("select parameter_value from application_settings where parameter_name = ?"))
                          qDebug() << "Prepare Failed: " << select.lastError();
                      select.bindValue(0, t_parametername);
                      
                      if (select.exec())
                      {
                          if (select.next())
                              return select.value(0).toString();
                          else
                              return QString();
                      }
                      else
                      {
                          qDebug() << "Last Error: " <<  select.lastError().databaseText() << "\n" << select.lastError().driverText() << "\n" << select.lastError().nativeErrorCode();
                      
                          QMessageBox::critical(nullptr, QObject::tr("Database Access Failed"), QString("Failed to access a saved setting. You may need to restart Project Notes.\n\nError:\n%1").arg(select.lastError().text()) );
                          return QString();
                      }
                      
                      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