The problem of stitching single and double quotes in SQL statements

Source: Internet
Author: User

single double quotes in MySQL use
MySQL uses the basic DML type of the SQL statement, "" and "is the default string label symbols. However, in the actual situation, the following situation often occurs:

One: Query situation

Select book_id from PD WHERE isbn= ' 9787542739810 ' and book_name= ' "dispel the charm" and "return to the charm";
In this case, the book_name corresponding string is not a problem, and the SQL statement does not have a syntax problem. But look at the following statement:

SQL, insert into yourtable (F1,F2) VALUES (+, ' abc ')

SELECT book_id from PD WHERE isbn= ' 9787542739810 ' and book_name= ' let's go! Ma Xiaoboro ';

Report exception: You have a error in your SQL syntax;

In this, you may say, you put the "let's go! Ma Xiaoboro" in the periphery of the two single quotation marks in double quotes do not do it. But the reality is that Murphy's law is the worst-case scenario. In many cases, we do not know the incoming, delete, change, check the string, whether it contains "," or both, so it is not easy to encounter single quotation marks replaced by double quotation marks, double quotation marks are replaced by single quotation marks. This will require the use of the SQL escape character .

Two: Insert Condition

string data is enclosed in single quotation marks, if the inserted data contains single quotation marks, you need to deal with, you can replace single quotation marks with two single quotation marks, in SQL, two consecutive single quotes represent a single quote character, for example
Insert into yourtable (F1,F2) VALUES (+, ' ab ' ' C ') indicate insert
New record F2 field AB ' C
Use the function replace to achieve this function: replace (YOURSTR, "'", "" ")

The problem of stitching single and double quotes in SQL statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.