Strong recommendation! Usage of single quotation marks and double quotation marks in SQL statements

Time:2021-8-3

Strong recommendation! Usage of single quotation marks and double quotation marks in SQL statements

Author: Peter Yong
Original text:https://www.cnblogs.com/peter…

As for the insert string, there are problems in this aspect (single quotation marks and double quotation marks). In fact, it is mainly because the data types and variables are making trouble. Let’s talk about it separately below. Although we are talking about the insert statement, the select, update and delete statements are the same.

If there is the following table:

mytabe
Field 1      username       String type (name)
Field 2      age            Numeric (age)
Field 3      birthday        Date type (birthday)
Field 4      marry          Boolean (married or not, true if married, false if not married)
Field 5      leixing         String type (type)

Insert string type

If you want to insert a person named Zhang Hong, because it is a string, you should add a single apostrophe around the name in the insert statement, and the numeric type can be without single quotation marks

For example:

strsql=“Insert   into   mytable(username)   Values ('zhang Hong ') "

If the name is now a variable thename, write

strsql=”Insert into mytable(username) values(‘” & thename & “')”

Description: & change to + sign, string connection

here

Insert   into   mytable(username)   values(‘    Is the part in front of Zhang Hong, and thename is a string variable,    ')

It’s the part behind Zhang Hong.

Replace thename variable with Zhang Hong, and then & connect the three segments to become

strsql=“Insert   into   mytable(username)   Values ('zhang Hong ') "

If you want to insert two fields, for example, the name is “Zhang Hong” and the type is “student”

strsql=“Insert   into   mytable(username,leixing)   Values ('zhang Hong ',' student ') "

If the name is now a variable thename and the type is also a variable thetype,

Then write:

strsql=”Insert into mytable(username,leixing) values(‘” & thename & “','” & thetype & “')”

As in the first example, replace thename and thetype, and then connect them with connectors to form the same string as above.

Insert numeric

If you insert a record with an age of 12, be careful not to add a single apostrophe to the number

strsql=“Insert into mytable(age) values(12)”

If age is now a variable theage, it is:

strsql=“Insert into mytable(age) values(“ & theage & “)”

here

Insert into mytable(age) values

(is the front part of 12, theage is the age variable, and) is the back part of 12.

Replace theage, then connect the three parts with the & connector, and it will become the same character as above.

Insert date type

Date type is similar to string type, but replace single apostrophe with # sign( However, a single apostrophe can also be used in the access database)

strsql=“Insert into mytable(birthday) values(#1980-10-1#)”

If you change to the date variable thedate

strsql=“Insert into mytable(birthday) values(#” & thedate & “#)”

Insert Boolean

Booleans are similar to numbers: they only have two values, true and false,

For example:

strsql=“Insert into mytable(marry) values(True)”

If you replace it with the boolean variable themarry

strsql=“Insert into mytable(birthday) values(” & themarry& “)”

Comprehensive indicationexample

Insert a record with the name of Zhang Hong and the age of 12

strsql=“Insert   into   mytable(username,age)   Values (‘zhang Hong ‘, 12) “

Pay close attention to the above formula: because the name is a string, Zhang Hong added a single apostrophe on both sides; Age is a number, so there is no single apostrophe.

If you replace it with the string variable thename and the numeric variable theage, it becomes:

strsql=“Insert into mytable(username,age) values(‘” & thename & “’,” & theage & “)”

Pay attention to the above formula. In short, after replacing variables and connecting them, complete the same string as above.

Tips

To change the following sentence questions into variables:

strsql=“Insert   into   mytable(username)   Values ('zhang Hong ') "

Step 1: erase Zhang Hong and add two quotation marks in the original position

strsql=“Insert into mytable(username) values(‘”   “')”

Step 2: add two connectors in the middle&

strsql=“Insert into mytable(username) values(‘” & & “')”

Step 3: write the variable between the two connectors

strsql=“Insert into mytable(username) values(‘” & thename & “')” -

When we write SQL queries, we still take the trouble to add single quotes. It seems that it doesn’t hurt. Because for query statements with string primary key, the performance with or without single quotation marks is 100 times better.

If there is any error, you are welcome to correct it. If you are helpful, you are welcome to like it and forward it to support.

More technical dry cargo official sharing, please pay attention to WeChat brother’s official account.Migrant workers’ road to technology

Strong recommendation! Usage of single quotation marks and double quotation marks in SQL statements

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]