How to insert a string containing '%' using QSqlQuery?

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.

Answers


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();

Need Your Help

Removing non-alphanumeric characters with sed

bash replace sed

I am trying to validate some inputs to remove a set of characters. Only alphanumeric characters plus, period, underscore, hyphen are allowed. I've tested the regex expression [^\w.-] here

Jsoup not getting full html

java jsoup

I am trying to Jsoup to parse the html from the URL http://www.threadflip.com/shop/search/john%20hardy

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.