Description of &, double quotation marks, and single quotation marks
Let's briefly explain their meaning.
(1) & is a concatenation operator. It can concatenate two strings into one. For example
A = "abc" & "def"
After the connection operation is executed, a = "abcdef ". Another example:
A = "abc" & temp
Note: Here temp is a string variable. If temp = "def", the result is:
A = "abc" & "def"
Continue to execute the join operation. The result is a = "abcdef ". Sometimes, there may be multiple join operators, as long as the operation in turn. For example
A = "abc" & "def" & "gh"
The result is a = "abcdefgh ".
(2) double quotation marks ". Double quotation marks must be added to both sides of the string to indicate that this is a string. The double quotation marks on both sides of "abc", "def", and "gh" indicate that this is a string.
Of course, no double quotation marks can be added to the string variable names. As mentioned above, temp in a = "abc" & temp is a variable,
Therefore, double quotation marks are not allowed on both sides. If you add double quotation marks to temp, it becomes a String constant "temp.
(3) single quotation marks '. Why is single quotes used? This is because if quotation marks are nested, the double quotation marks in the inner layer should be changed to single quotation marks.
Here is an example:
A = "Double quotation marks (such as" abc ") must be added to both sides of the string, or ...... "
As you can see, the original meaning is: the first and last "indicates a string in the middle. The double quotation marks on both sides of "abc" indicate that abc is a string. However, an error occurs. Because 1st double quotes will actually match 2nd double quotes, not the expected 4th double quotes.
In this case, you need to change the double quotation marks "abc" in the inner layer to single quotation marks, as shown below:
A = "Double quotation marks (such as 'abc') must be added to both sides of the string ...... "
The following is a supplement:
1) all the symbols mentioned above are in English and are used in syntax. If it is a chinese symbol, you do not need to change it. For example;
A = "Mr. Wang said," Let's eat ". "
The Chinese characters do not participate in the syntax, so they do not need to be changed.
2) When quotation marks are nested, the inner quotation marks are generally changed to single quotation marks, but you can also change them to two double quotation marks, as shown below:
A = "Double quotation marks (" abc "") must be added to both sides of the string, for example, "abc" ", or ...... "
Double quotation marks, single quotation marks, and &
(1) first, let's look at the simplest SQL statement.
Insert Into users (username) values ("John ")
Let's take a look. This is a standard SQL statement. Because username is a text field, double quotation marks must be added on both sides of the field value to indicate that John is a string.
(2) but in ASP, we usually write
StrSql = "Insert Into users (username) values ('John ')"
In this case, the double quotation marks indicate a string in the middle. The single quotation marks on both sides of the King are nested because of the quotation marks, so the inner quotation marks should be single quotation marks.
(3) during actual insertion, because John is usually a variable obtained from the form, but because the variable cannot be directly written into the string, it must be connected together with the string through a connector, therefore, it must be written as follows:
Myusername = Request. Form ("username ")
......
StrSql = "Insert Into users (username) values ('" & myusername &"')"
Many people are confused at this time. Why are there single quotation marks and double quotation marks. A simple look at the above formula is actually composed of the following three parts:
String constant: "Insert Into users (username) values ('"
String variable: myusername
String constant :"')"
It may be difficult to directly understand the above formula. Now we can see the value of myusername. Assume that myusername = "Xiao Wang" is substituted:
StrSql = "Insert Into users (username) values ('" & "John "&"')"
Then execute the join operators in sequence and the result is:
StrSql = "Insert Into users (username) values ('John ')"
This is the correct SQL statement mentioned in (2. At this point, we can see that the double quotation marks (1st) and two matching double quotation marks (double quotation marks) in the substatement indicate that this is a string. The double quotation marks (3rd) and four matching double quotation marks (4) indicate a string. The single quotation marks in the formula are the single quotation marks before and after the text field value Mr. Wang.
(4) There is only one field above. here we can see that there are two fields:
StrSql = "Insert Into users (username, password) values ('" & myusername &"','"&
Mypassword &"')"
Here, myusername and mypassword are variables. If myusername = "", mypassword = "123456" is entered, the result is:
StrSql = "Insert Into users (username, password) values ('" & "John" & "', '" & "123456 "&"')"
Perform the join operation in sequence and the result is:
StrSql = "Insert Into users (username, password) values ('John ', '123 ')"
(5) The text field value is enclosed by quotation marks, and the date field is enclosed by #. No quotation marks are required on both sides of the number and logic. For example, age is a number and submit_date is a date type.
StrSql = "Insert Into users (username, password, age, submit_date) values ('" & myusername & "', '" & mypassword & "'," & myage &", # "& mysubmit_date &"#)"
Note that the corresponding symbols are added or not added based on the field type. Assume that myusername = "", mypassword = "123456", myage = 20, mysubmit_date = "2004-4-1", and the result is:
StrSql = "Insert Into users (username, password, age, submit_date) values ('" & John "&"', '"&" 123456 "&"', "& 20 &", # "&" 2004-4-1 "&"#)"
Here 20 is very special. During the operation, it is automatically converted to a string because all involved in the operation are strings. Result:
StrSql = "Insert Into users (username, password, age, submit_date) values ('" & John "&"', '"&" 123456 "&"', "&" 20 "&", # "&" 2004-4-1 "&"#)"
Perform the join operation in sequence.
StrSql = "Insert Into users (username, password, age, submit_date) values ('John ', '123', 20, #2004-4-1 #) "This is the standard SQL statement we need.
AboutInsertString
Many of you have encountered problems in this regard, mainly because the data types and variables are being blamed. The following describes the Insert statements, but the Select, Update, and Delete statements are the same. Assume that the following table is available:
Mytabe
Field 1 username string type (name)
Field 2 age numeric type (age)
Field 3 birthday (birthday)
Field 4 marry Boolean (whether to get married, True for marriage, False for not married)
Field 5 leixing string type (type)
1. Insert string type
If you want to Insert a person named zhanghong, because it is a string, you must add a single marker on both sides of the name in the Insert statement, for example:
Strsql = "Insert into mytable (username) values ('zhanghong ')"
If the current name is a variable thename, it is written
Strsql = "Insert into mytable (username) values ('" & thename & "')"
Here, Insert into mytable (username) values ('is the part before zhanghong, thename is the string variable,') is the part after zhanghong. Replace the thename variable with zhanghong, and then use & to connect the three sections.
Strsql = "Insert into mytable (username) values ('zhanghong ')".
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', 'Students ')"
If the current name is a variable thename and the type is also a variable thetype, it is written:
Strsql = "Insert into mytable (username, leixing) values ('" & thename & "," & thetype & "')"
Like in the first example, after thename and thetype are replaced, connect to the same string as above with a connector.
2. Insert a number
If you insert a record of the age of 12, note that you do not need to add a single marker for the number:
Strsql = "Insert into mytable (age) values (12 )"
If the current age is a variable theage, it is:
Strsql = "Insert into mytable (age) values (" & theage & ")"
Insert into mytable (age) values (before 12, theage is the age variable,) is after 12. Replace theage and connect the three parts with the & connector to the same character as above.
3. insert date type
The date type is similar to the string type, but you need to replace the single marker with the # marker. (However, you can use a single marker 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 & "#)"
4. Insert a Boolean
The Boolean and numeric types are similar: only two values are True and False, for example:
Strsql = "Insert into mytable (marry) values (True )"
If you replace themarry with a Boolean variable
Strsql = "Insert into mytable (birthday) values (" & themarry & ")"
5. Comprehensive examples
Insert a record named zhanghong, age 12, and birth date
Strsql = "Insert into mytable (username, age, birthday) values ('zhanghong', 12, #1970-6-8 #)"
Note the preceding formula: because the name is a string, a single marker is added to both sides of Zhang Hong; the age is a number, so no single marker is added. If thename, theage, and thebirthday are changed:
Strsql = "Insert into mytable (username, age, birthday) values ('" & thename & "," & theage & ", #" & thebirthday & "#)"
In short, replace the variable and complete the same string as the above after the connection.
6. Tips
(1) Insert a string
Replace the following statement into a variable:
Strsql = "Insert into mytable (username) values ('zhanghong ')"
Step 1: First 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 two connectors
Strsql = "Insert into mytable (username) values ('" & thename & "')"
(2) Insert numeric/boolean values
Replace the following statement into a variable:
Strsql = "Insert into mytable (age) values (12 )"
Step 1: First erase 12 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 two connectors
Strsql = "Insert into mytable (username) values (" & theage & ")"
(3) Insert date type
Replace the following statement into a variable:
Strsql = "Insert into mytable (birthday) values (#1980-10-1 #)"
Step 1: First erase 1980-10-1 and add two quotation marks to 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 two connectors
Strsql = "Insert into mytable (username) values (#" & thedate & "#)"