February 1, 2012

umen242 umen242
Lab Rat
344 posts

how to deal with comma and apostrophe chars when inserting into SQLight

 

i have string that contains apostrophe and comma’s and when i excute insert into SQLight it gives me error for example with string like this :

  1. ...., 'The Smiths - I Know It's Over', .....
  2. "Over": syntax error Unable to execute statement

how can i or what can i do to keep the apostrophe’s in the string but preform valid insert? .
im using simple :

  1. QString InsertSqlStst =" INSERT ......"
  2. QSqlQuery query;
  3. ret = query.exec(InsertSqlStst);

5 replies

February 1, 2012

leon.anavi leon.anavi
Mad Scientist
1200 posts

You are talking about SQLite, aren’t you?

Please have a look at SQLite documentation [sqlite.org]:

A single quote within the string can be encoded by putting two single quotes in a row – as in Pascal.

 Signature 

http://anavi.org/

February 1, 2012

umen242 umen242
Lab Rat
344 posts

yeah SQLite , using the Qt tools to connect

  1. m_db = QSqlDatabase::addDatabase("QSQLITE");

that means i need to run over each string to check ?
isn’t there some build in solution ?

February 1, 2012

leon.anavi leon.anavi
Mad Scientist
1200 posts
umen242 wrote:
that means i need to run over each string to check ? isn’t there some build in solution ?

Use QSqlDriver::formatValue [developer.qt.nokia.com] According to Qt documentation it will do the job:

Any embedded single-quote characters are escaped (replaced with two single-quote characters).

 Signature 

http://anavi.org/

February 1, 2012

Volker Volker
Ant Farmer
5428 posts

Use placeholders and bind them instead of handcrafting things. QSqlQuery docs have the gory details.

February 1, 2012

umen242 umen242
Lab Rat
344 posts

i just looked the :formatValue source code , its using simple replace
(: and this is what i did.
Thanks for helping guys

 
  ‹‹ [Solved] setWindowTitle was not declared in this scope      QFileDialog: strange different behaviour of AnyFile and ExistingFile ››

You must log in to post a reply. Not a member yet? Register here!