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

前端 未结 5 1738
南旧
南旧 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:07

    Yo need to escape the ' character with a backslash \

    Women\'s Development & Empowerment, Youth Affairs

    0 讨论(0)
  • 2020-12-19 02:18

    You need to escape your quote. You can do this by doubling it in your insert query; i.e. use '' rather than '. That is two single quotes rather than a single double quote.

    0 讨论(0)
  • 2020-12-19 02:22

    You will have to escape the input strings before passing them to MySql.

    The list of escape characters is:

    Character   Escape Sequence
    \0  An ASCII NUL (0x00) character.
    \'  A single quote (“'”) character.
    \"  A double quote (“"”) character.
    \b  A backspace character.
    \n  A newline (linefeed) character.
    \r  A carriage return character.
    \t  A tab character.
    \Z  ASCII 26 (Control-Z). See note following the table.
    \\  A backslash (“\”) character.
    \%  A “%” character. See note following the table.
    \_  A “_” character. See note following the table.
    
    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-12-19 02:25

    What is the server side languge you using?

    there are many methods through which you can escape string , i'll prefer that!

    Eg: you can use mysql_escape_string the data you are entering in query so it will automatically escape charecters like " ' "

    mysql_escape_string is a php function provided you are using php

    0 讨论(0)
提交回复
热议问题