How do I insert a special character such as ' into MySQL?

前端 未结 5 1754
南旧
南旧 2020-12-19 01:50

I am executing the insert query from a shell script which reads data from multiple files. Some of the data to be inserted contains \' in the text a

5条回答
  •  醉酒成梦
    2020-12-19 02:23

    You need to escape the quote, like so:

    'Women\'s Development & Empowerment, Youth Affairs'
    

    Note, that if you're generating the SQL statement from a language like PHP, there are functions available to do this for you.

    In PHP, for instance, there is mysql_real_escape_string, which takes care of it for you. Note, that prepared statements are to be prefered over this, as it's harder to get those wrong.

    See also:

    • The MySQL manual entry on strings
    • PHP PDO prepared statements

提交回复
热议问题