Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:31:51+00:00 2026-05-31T20:31:51+00:00

I’m doing an insert: QSqlQuery myQuery(db); myQuery.prepare(INSERT INTO mytable VALUES (:val1, :val2)); myQuery.bindValue(:val1, 1);

  • 0

I’m doing an insert:

QSqlQuery myQuery(db);
myQuery.prepare("INSERT INTO mytable VALUES (:val1, :val2)");
myQuery.bindValue(":val1", 1);
myQuery.bindValue(":val2", 2);
myQuery.exec();

Then I need to get the executed SQL query for logging purposes.

myQuery.executedQuery() returns "INSERT INTO mytable VALUES (?, ?)".

How do I to get executed query with the actual binded values that were used?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:31:52+00:00Added an answer on May 31, 2026 at 8:31 pm

    An alternative to what lightstep suggested, is to prepare query strings and then call a function that first writes the query to the log and only then calls real execute(). I personally use QString::arg() and “%number” for arguments to make a query string instead of bindValue().

    Let’s sum things up:

    Solution #1 (lightstep)

    I came up with this workaround:

    QString getLastExecutedQuery(const QSqlQuery& query)
    {
     QString str = query.lastQuery();
     QMapIterator<QString, QVariant> it(query.boundValues());
     while (it.hasNext())
     {
      it.next();
      str.replace(it.key(),it.value().toString());
     }
     return str;
    }
    

    Solution #2 (me):

    // my helper function
    
    #define SQLDB_SHOW_QUERIES
    #define SQLDB_LOG_QUERIES
    #define SQLDB_LOG_FILENAME "sqlite.db.log"
    
    bool executeQuery(QSqlQuery& queryObject, const QString& query)
    {
     bool result = true;;
    #ifdef SQLDB_SHOW_QUERIES
     std::cout<<query.toStdString()<<std::endl;
    #endif
    #ifdef SQLDB_LOG_QUERIES
     std::fstream fs_log;
     fs_log.open(SQLDB_LOG_FILENAME,std::ios::out|std::ios::app);
     if (fs_log.is_open())
     {
      fs_log<<query.toUtf8().data()<<std::endl;
     }
    #endif
     result &= queryObject.exec(query);
    #ifdef SQLDB_SHOW_QUERIES
     if (!result) std::cout<<queryObject.lastError().text().toStdString()<<std::endl;
     std::cout<<std::endl;
    #endif
    #ifdef SQLDB_LOG_QUERIES
     if (fs_log.is_open())
     {
      if (!result) fs_log<<queryObject.lastError().text().toUtf8().data()<<std::endl;
      fs_log<<std::endl;
      fs_log.close();
     }
    #endif
     return result;
    }
    
    // your sample code
    
    QSqlQuery myQuery(db);
    QString query = QString("INSERT INTO mytable VALUES (%1,%2)")
     .arg(1).arg(2);
    executeQuery(myQuery,query);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.