<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[bindValue is not working]]></title><description><![CDATA[<pre><code>bool DbManager::add_value(const QString&amp; col, const auto&amp; value){
        bool success = false;
        QSqlQuery query;  
        query.prepare("INSERT INTO idcard_data (Accountid) Values (:ac)");
        query.bindValue(":ac","001"); 
  
        if (query.exec()){
            success = true;
        }
        else qDebug()&lt;&lt;"INSERT error"&lt;&lt;query.lastError();
        qDebug() &lt;&lt; query.executedQuery();
        return success;
    }
</code></pre>
<p dir="auto">debug return shows the value I want to bind is ?, I have tried pass Qstring&amp; variable, still the same, but it works when I just run query.prepare with manually input data and not using bindvalue.<br />
"INSERT INTO idcard_data (Accountid) Values (?)"</p>
<p dir="auto">I have searched many posts that reported to have this issue but couldn't find any legit solution, I hope if someone know what happening can give me some helps, thanks a lot.</p>
<p dir="auto">Edit: if I use Qstring&amp; variable like this,</p>
<pre><code>query.prepare("INSERT INTO idcard_data (:A) Values (:ac)");
        query.bindValue(":A",col);
        query.bindValue(":ac","hello");
</code></pre>
<p dir="auto">it gives me this error:</p>
<pre><code>INSERT error QSqlError("", "Parameter count mismatch", "")
"INSERT INTO idcard_data (?) Values (?)"
</code></pre>
]]></description><link>https://forum.qt.io/topic/145532/bindvalue-is-not-working</link><generator>RSS for Node</generator><lastBuildDate>Sat, 16 May 2026 15:18:41 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/145532.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 02 Jun 2023 07:35:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 09:27:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/icoicqico">@<bdi>icoicqico</bdi></a><br />
If you want help, e.g. from <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> or myself, let us know when you are prepared to make the changes we suggested, and not continue with something else which will never work.  Up to you.</p>
]]></description><link>https://forum.qt.io/post/760331</link><guid isPermaLink="true">https://forum.qt.io/post/760331</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 02 Jun 2023 09:27:14 GMT</pubDate></item><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 09:25:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/miabaker">@<bdi>miabaker</bdi></a> said in <a href="/post/760327">bindValue is not working</a>:</p>
<blockquote>
<p dir="auto">Hi, I think you need to use the QSqlQuery::bindValue() method instead of QSqlQuery::addBindValue().</p>
</blockquote>
<p dir="auto">So far the OP has only used <code>bindValue()</code>, which is correct for named parameters, not <code>addBindValue()</code> at all which is only for positional parameters.  So don't know what you are saying here.</p>
]]></description><link>https://forum.qt.io/post/760330</link><guid isPermaLink="true">https://forum.qt.io/post/760330</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 02 Jun 2023 09:25:47 GMT</pubDate></item><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 09:10:38 GMT]]></title><description><![CDATA[<p dir="auto">Hi, I think you need to use the QSqlQuery::bindValue() method instead of QSqlQuery::addBindValue(). The former binds a value to a named placeholder, while the latter binds a value to a positional placeholder. See the documentation for more details: <a href="https://doc.qt.io/qt-5/qsqlquery.html#bindValue" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qsqlquery.html#bindValue</a></p>
<p dir="auto">Regards, Mia from <a href="https://codeit.us/" target="_blank" rel="noopener noreferrer nofollow ugc">CodeIT</a></p>
]]></description><link>https://forum.qt.io/post/760327</link><guid isPermaLink="true">https://forum.qt.io/post/760327</guid><dc:creator><![CDATA[miabaker]]></dc:creator><pubDate>Fri, 02 Jun 2023 09:10:38 GMT</pubDate></item><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 08:57:24 GMT]]></title><description><![CDATA[<p dir="auto">A column can not be bound, only values, I'm pretty sure query.exec() returns false but you ignore me.</p>
]]></description><link>https://forum.qt.io/post/760323</link><guid isPermaLink="true">https://forum.qt.io/post/760323</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 02 Jun 2023 08:57:24 GMT</pubDate></item><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 08:54:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/icoicqico">@<bdi>icoicqico</bdi></a> said in <a href="/post/760315">bindValue is not working</a>:</p>
<blockquote>
<p dir="auto">"INSERT INTO idcard_data (?) Values ('hello')"</p>
</blockquote>
<p dir="auto">This is the result of query.executedQuery(), or you want something else?</p>
]]></description><link>https://forum.qt.io/post/760321</link><guid isPermaLink="true">https://forum.qt.io/post/760321</guid><dc:creator><![CDATA[icoicqico]]></dc:creator><pubDate>Fri, 02 Jun 2023 08:54:15 GMT</pubDate></item><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 08:46:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/icoicqico">@<bdi>icoicqico</bdi></a><br />
You reply to me, where I just wrote:</p>
<blockquote>
<blockquote>
<p dir="auto"><code>query.prepare("INSERT INTO idcard_data (:A) Values (:ac)");</code></p>
</blockquote>
<p dir="auto">Don't waste time on this, it is wrong and will not work.</p>
</blockquote>
<p dir="auto">There is no point you doing this again and show the error it produces given that this is not allowed.  Did you read that I wrote this?</p>
<p dir="auto">Both <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> and I have now said repeatedly to check the result from any <code>prepare()</code> statements.  If you want help why do you simply ignore this?  It's pretty frustrating for responders to ask you questions or tell you what to do/not do if you're not going to act on these.</p>
<p dir="auto">The only one we are interested in at present is your original:</p>
<pre><code>        query.prepare("INSERT INTO idcard_data (Accountid) Values (:ac)");
        query.bindValue(":ac","001"); 
</code></pre>
]]></description><link>https://forum.qt.io/post/760318</link><guid isPermaLink="true">https://forum.qt.io/post/760318</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 02 Jun 2023 08:46:26 GMT</pubDate></item><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 08:33:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> It is not the type problem, ok let's use 'hello' as an example,</p>
<pre><code>query.prepare("INSERT INTO idcard_data (Accountid) Values ('hello')");
</code></pre>
<p dir="auto">return:</p>
<pre><code>"INSERT INTO idcard_data (Accountid) Values ('hello')"
</code></pre>
<p dir="auto">And I check my database the value "hello" is updated.</p>
<p dir="auto">But if I do it this way, col is "Accountid"</p>
<pre><code>query.prepare("INSERT INTO idcard_data (:A) Values ('hello')");
query.bindValue(":A",col);
</code></pre>
<pre><code>INSERT error QSqlError("", "Parameter count mismatch", "")
"INSERT INTO idcard_data (?) Values ('hello')"
</code></pre>
<p dir="auto">and no update on the databse.</p>
]]></description><link>https://forum.qt.io/post/760315</link><guid isPermaLink="true">https://forum.qt.io/post/760315</guid><dc:creator><![CDATA[icoicqico]]></dc:creator><pubDate>Fri, 02 Jun 2023 08:33:26 GMT</pubDate></item><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 08:26:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/icoicqico">@<bdi>icoicqico</bdi></a><br />
Firstly, as <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a>  says.  Put in error checking.</p>
<blockquote>
<p dir="auto"><code>query.prepare("INSERT INTO idcard_data (:A) Values (:ac)");</code></p>
</blockquote>
<p dir="auto">Don't waste time on this, it is wrong and will not work.</p>
<blockquote>
<pre><code>    query.prepare("INSERT INTO idcard_data (Accountid) Values (:ac)");
    query.bindValue(":ac","001");
</code></pre>
</blockquote>
<blockquote>
<p dir="auto">I just run query.prepare with manually input data</p>
</blockquote>
<p dir="auto">What type is <code>AccountId</code> in your database?  Have you really chosen a (3-character) <em>string</em> as your <code>Accountid</code>?  Show your code for this "manually input data" case.</p>
]]></description><link>https://forum.qt.io/post/760314</link><guid isPermaLink="true">https://forum.qt.io/post/760314</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 02 Jun 2023 08:26:03 GMT</pubDate></item><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 08:20:50 GMT]]></title><description><![CDATA[<p dir="auto">So when no error is printed then query.exec() succeeded and all is fine.</p>
<p dir="auto">You still not check the return value of query.prepare()</p>
]]></description><link>https://forum.qt.io/post/760313</link><guid isPermaLink="true">https://forum.qt.io/post/760313</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 02 Jun 2023 08:20:50 GMT</pubDate></item><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 08:08:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> Thanks for the reply, no last error is printed, and I have checked the database no correct value is added. But if I just manually input the data to the statement without bindValue the data is correctly update.</p>
]]></description><link>https://forum.qt.io/post/760311</link><guid isPermaLink="true">https://forum.qt.io/post/760311</guid><dc:creator><![CDATA[icoicqico]]></dc:creator><pubDate>Fri, 02 Jun 2023 08:08:24 GMT</pubDate></item><item><title><![CDATA[Reply to bindValue is not working on Fri, 02 Jun 2023 07:53:35 GMT]]></title><description><![CDATA[<p dir="auto">Please adjust your post so the code can be read by others - please use the code tags.</p>
<p dir="auto">And for your code - please check the return value of query.prepare(). The statement with '?' is correct. What does query.lastError() print?</p>
]]></description><link>https://forum.qt.io/post/760309</link><guid isPermaLink="true">https://forum.qt.io/post/760309</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 02 Jun 2023 07:53:35 GMT</pubDate></item></channel></rss>