I got the select and insert worked out.
Insert now works with bound values.
But now I can't get the update working - I get the same error "parameter count mismatch".
Relevant code is below. Note that the QSqlQuery is returned as a pointer from the Contacts Class and executed in main.
Contacts class
{
if (contactObjectInUse)
{
QString updateStatement
= "\"update Contacts set ";
if (nameInUse)
updateStatement += "name = :name, ";
if (urlInUse)
updateStatement += "url = :url, ";
if (contactTypeInUse)
updateStatement += "contactType = :contactType, ";
updateStatement += "where contactID = :contactID\"";
int i = updateStatement.indexOf(", where");
// strip the final comma out
if (i >= 0)
updateStatement.remove(i, 1);
aQuery->prepare(updateStatement);
cout << "Contacts update statement: " << qPrintable(updateStatement) << endl;
if (nameInUse)
aQuery->bindValue(":name", name);
if (urlInUse)
aQuery->bindValue(":url", url);
if (contactTypeInUse)
aQuery->bindValue(":contactType", contactType);
aQuery->bindValue(":contactID", contactID);
}
return aQuery;
QSqlQuery* Contacts::getUpdateSQLQueryPtr()
{
QSqlQuery *aQuery = 0;
if (contactObjectInUse)
{
aQuery = new QSqlQuery();
QString updateStatement = "\"update Contacts set ";
if (nameInUse)
updateStatement += "name = :name, ";
if (urlInUse)
updateStatement += "url = :url, ";
if (contactTypeInUse)
updateStatement += "contactType = :contactType, ";
updateStatement += "where contactID = :contactID\"";
int i = updateStatement.indexOf(", where");
// strip the final comma out
if (i >= 0)
updateStatement.remove(i, 1);
aQuery->prepare(updateStatement);
cout << "Contacts update statement: " << qPrintable(updateStatement) << endl;
if (nameInUse)
aQuery->bindValue(":name", name);
if (urlInUse)
aQuery->bindValue(":url", url);
if (contactTypeInUse)
aQuery->bindValue(":contactType", contactType);
aQuery->bindValue(":contactID", contactID);
}
return aQuery;
To copy to clipboard, switch view to plain text mode
Note: name, url and contactType are all QStrings. contactID is an int.
main
QPointer<Contacts> aContact = new Contacts();
aQueryPtr->exec("select url, contactType from contacts where contactID = 4");
aQueryPtr->next();
// set aContact to selected values;
aContact->setContactID(4);
aContact->setUrl(aQueryPtr->value(0).toString());
aContact->setContactType(aQueryPtr->value(1).toString());
// set aContact variables to new values;
aContact->setUrl("www.a_really_cool_url");
aQueryPtr = aContact->getUpdateSQLQueryPtr();
// check bound values
QMapIterator<QString, QVariant> i(aQueryPtr->boundValues());
while (i.hasNext())
{
i.next();
cout << qPrintable(i.key()) << ": "
<< qPrintable(i.value().toString()) << endl;
}
int numBoundValues = aQueryPtr->boundValues().size();
cout <<
"Number of bound values = " << qPrintable
(QString::number(numBoundValues
)) << endl;
//int paramCount = sqlite3_bind_parameter_count(aQueryPtr);
//cout << "SQLite number of bound values = " << qPrintable(QString::number(paramCount)) << endl;
aQueryPtr->exec();
cout << " QUERY IS: " << qPrintable(aQueryPtr->lastQuery()) << endl;
delete aContact;
QPointer<Contacts> aContact = new Contacts();
aQueryPtr = new QSqlQuery();
aQueryPtr->exec("select url, contactType from contacts where contactID = 4");
aQueryPtr->next();
// set aContact to selected values;
aContact->setContactID(4);
aContact->setUrl(aQueryPtr->value(0).toString());
aContact->setContactType(aQueryPtr->value(1).toString());
// set aContact variables to new values;
aContact->setUrl("www.a_really_cool_url");
aQueryPtr = aContact->getUpdateSQLQueryPtr();
// check bound values
QMapIterator<QString, QVariant> i(aQueryPtr->boundValues());
while (i.hasNext())
{
i.next();
cout << qPrintable(i.key()) << ": "
<< qPrintable(i.value().toString()) << endl;
}
int numBoundValues = aQueryPtr->boundValues().size();
cout << "Number of bound values = " << qPrintable(QString::number(numBoundValues)) << endl;
//int paramCount = sqlite3_bind_parameter_count(aQueryPtr);
//cout << "SQLite number of bound values = " << qPrintable(QString::number(paramCount)) << endl;
aQueryPtr->exec();
cout << " QUERY IS: " << qPrintable(aQueryPtr->lastQuery()) << endl;
delete aContact;
To copy to clipboard, switch view to plain text mode
Also I tried using sqlite3_bind_parameter_count to see how many bound values SQLite thought it had but I got an error:
'sqlite3_bind_parameter_count was not declared in this scope'.
How do I fix that?
The output is below
:contactID: 4
:contactType: web
:url: www.a_really_cool_url
Number of bound values = 3
QUERY IS: "update Contacts set url = :url, contactType = :contactType where contactID = :contactID"
ROLLBACK - Parameter count mismatch
Thanks for all the help
Jeff
Bookmarks