In SQL, strings are delimited with '
, not "
.
Anyway, you should use parameters:
query.prepare("INSERT INTO my_table (name, address) VALUES (?, ?)");
query.addBindValue("Harry");
query.addBindValue("#302%%1,...");
query.exec();
Question
I am trying to insert a string which contains '%' using QsqlQuery but I am always getting an error of type QSqlError::TransactionError with the message
"near "%": syntax error Unable to execute statement"
I have tried several ways to escape the "%" but I am still getting that error message. Please see the code snippet below.
query.exec(QString("INSERT INTO my_table (name, address) VALUES (\"%1\", \"%2\")")
.arg("Harry", "#302%%1,..."));
In place of "%%", I have tried "\%", "\%", but still it didn't work and I am having the same error message.
Solution
In SQL, strings are delimited with '
, not "
.
Anyway, you should use parameters:
query.prepare("INSERT INTO my_table (name, address) VALUES (?, ?)");
query.addBindValue("Harry");
query.addBindValue("#302%%1,...");
query.exec();