Use single or double quotes when stitching SQL statements?

Source: Internet
Author: User

SQL statements using basic DML types in MySQL, "and" are default string label symbols. In reality, however, the following situations often occur:

One: Query situation

SELECT book_id from PD WHERE isbn= ' 123456789 ' and Book_name= ' "China" and "cloud-dwelling community";

In this case, the book_name corresponding string is not problematic and the SQL statement has no syntax problems. But look at one of the following statements:

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

SELECT book_id from PD WHERE isbn= ' 123456789 ' and book_name= ' let ' S go! cloud-dwelling communities ';

Report exception: You have a error in your SQL syntax;
Here, you might say, you put the two single quotes from the periphery of the ' let ' go! cloud-dwelling community ' into double quotes. But the reality is often based on Murphy's law towards the worst. In many cases, we don't know whether the incoming, deleted, modified, and searched strings contain "," or both, so it is not easy to encounter single quotes replaced with double quotes, and double quotes are replaced with single quotes. This requires the use of SQL escape characters.

Two: Insert situation

String data is enclosed in single quotes, if the inserted data contains single quotes that need to be processed, you can replace single quotes with two single quotes, and two single quotes in SQL for a single quote character, such as

Insert into yourtable (F1,F2) VALUES (+, ' ab ' ' C ') represents inserting

New record F2 field AB ' C

Use function Replace to achieve this function: replace (YOURSTR, "'", "" ")

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.