For example:
select * from tablename where fields like "%string "hi" %";
Error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'hi" "' at line 1
How do I build this query?
See http://dev.mysql.com/doc/refman/5.0/en/string-literals.html
So you need
Although as Bill Karwin notes below, using double quotes for string delimiters isn't standard SQL, so it's good practice to use single quotes. This simplifies things:
MySQL has the string function QUOTE, and it should solve this problem:
For testing how to insert the double quotes in MYSQL using Terminal you can use following way.
after inserting the value you can update the value in the db with double quotes or single quotes
I've developed my own MySQL escape method in Java (if useful for anyone).
See class code below.
Warning: wrong if NO_BACKSLASH_ESCAPES SQL mode is enabled.
You can use mysql_real_escape_string.
mysql_real_escape_string()
does not escape%
and_
, so you should escape MySQL wildcards (%
and_
) separately.You should use single-quotes for string delimiters. The single-quote is the standard SQL string delimiter, and double-quotes are identifier delimiters (so you can use special words or characters in the names of tables or columns).
In MySQL, double-quotes work (nonstandardly) as a string delimiter by default (unless you set
ANSI
SQL mode). If you ever use another brand of SQL database, you'll benefit from getting into the habit of using quotes standardly.Another handy benefit of using single-quotes is that the literal double-quote characters within your string don't need to be escaped: