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 weird error
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery weird error

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 265 Views
  • 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.
  • krzysieklfcK Offline
    krzysieklfcK Offline
    krzysieklfc
    wrote on last edited by krzysieklfc
    #1

    In the following code, q.prepare() and q.exec() both return false and qDebug outputs - " Parameter count mismatch"

    void Database::deleteAllRecords(const QString& db_name, const QString& table)
    {
    	auto err = initDb("deleteData", db_name);
    	Q_ASSERT(err.type() == QSqlError::NoError);
    
    	auto db = QSqlDatabase::database("deleteData");
    
    	auto q = QSqlQuery{ db };
    	Q_ASSERT(q.driver()->hasFeature(QSqlDriver::NamedPlaceholders));
    
    	auto zxda = q.prepare(QLatin1String{ R"(
    		DELETE FROM :table
    	)" });
    
    	//the same with addBindValue
    	q.bindValue(":table", table);
    
    	auto ok = q.exec();
    
    	qDebug() << q.lastError().text();
    }
    

    but when I supply hardcoded table name then it all works just fine:

    void Database::deleteAllRecords(const QString& db_name)
    {
    	auto err = initDb("deleteData", db_name);
    	Q_ASSERT(err.type() == QSqlError::NoError);
    
    	auto db = QSqlDatabase::database("deleteData");
    
    	auto q = QSqlQuery{ db };
    	Q_ASSERT(q.driver()->hasFeature(QSqlDriver::NamedPlaceholders));
    
    	auto zxda = q.prepare(QLatin1String{ R"(
    		DELETE FROM cache
    	)" });
    
    	auto ok = q.exec();
    
    	qDebug() << q.lastError().text();
    }
    
    

    Db driver is QSQLITE. Does anyone have an idea what's going on there?

    JonBJ 1 Reply Last reply
    0
    • krzysieklfcK krzysieklfc

      In the following code, q.prepare() and q.exec() both return false and qDebug outputs - " Parameter count mismatch"

      void Database::deleteAllRecords(const QString& db_name, const QString& table)
      {
      	auto err = initDb("deleteData", db_name);
      	Q_ASSERT(err.type() == QSqlError::NoError);
      
      	auto db = QSqlDatabase::database("deleteData");
      
      	auto q = QSqlQuery{ db };
      	Q_ASSERT(q.driver()->hasFeature(QSqlDriver::NamedPlaceholders));
      
      	auto zxda = q.prepare(QLatin1String{ R"(
      		DELETE FROM :table
      	)" });
      
      	//the same with addBindValue
      	q.bindValue(":table", table);
      
      	auto ok = q.exec();
      
      	qDebug() << q.lastError().text();
      }
      

      but when I supply hardcoded table name then it all works just fine:

      void Database::deleteAllRecords(const QString& db_name)
      {
      	auto err = initDb("deleteData", db_name);
      	Q_ASSERT(err.type() == QSqlError::NoError);
      
      	auto db = QSqlDatabase::database("deleteData");
      
      	auto q = QSqlQuery{ db };
      	Q_ASSERT(q.driver()->hasFeature(QSqlDriver::NamedPlaceholders));
      
      	auto zxda = q.prepare(QLatin1String{ R"(
      		DELETE FROM cache
      	)" });
      
      	auto ok = q.exec();
      
      	qDebug() << q.lastError().text();
      }
      
      

      Db driver is QSQLITE. Does anyone have an idea what's going on there?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @krzysieklfc
      I could be off, because I've never used SQLite, but you can only put placeholder-bind-variables where the SQL actually allows it, it's not just "text-like-macro-substitution". I think you will find DELETE FROM <table-name> does not allow a "bound variable" there, it has to be literal, hence the behaviour you see. Essentially I think you can use the bound-variables for column values & literals, but not e.g. for a table or database name etc.

      1 Reply Last reply
      3
      • krzysieklfcK Offline
        krzysieklfcK Offline
        krzysieklfc
        wrote on last edited by
        #3

        Thanks for reply.

        I managed to solve this problem with the following:

        	auto prepare_string = QString{ "DELETE FROM %1" }.arg(table);
        	auto ok = q.prepare(prepare_string);
        
        	auto ok = q.exec();
        
        
        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