NeDark
NeDark

Reputation: 1294

Escaping string for SQL in C++

I am searching a simple "addslashes" function for a program that must save in a sqlite database some information.

Thanks.

Upvotes: 1

Views: 4429

Answers (3)

Nightmare_IntMain
Nightmare_IntMain

Reputation: 74

I think its echo " \\hi "; - ouput \hi

Upvotes: -5

Artyom
Artyom

Reputation: 31233

  1. I'd suggest to use prepared statement and data binding to query so you would not need escaping or use a library like CppDB or SOCI to do it easily
  2. Sqlite3 uses SQL standard quites, so for escaping text you need to "double the quotes" for blob you need hexadecimal representation, i.e.

    C string: char const *s="I'm" -> SQL: 'I''m'
    C blob  : char s[2]={0xFF,0}  -> SQL: x'FF00'
    

    See: http://www.sqlite.org/lang_expr.html

Upvotes: 2

Jeff Hubbard
Jeff Hubbard

Reputation: 9892

Instead of trying to re-implement addslashes, you should instead look into using prepared statements. They're simpler, faster, and easier.

Upvotes: 4

Related Questions