How to process single quotes and double quotation marks in SQL statements

Source: Internet
Author: User

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 to form 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, the type is also a variable thetype,
Written as: 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 and aged 12
Strsql = "Insert into mytable (username, age) values ('zhanghong', 12 )"
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 you replace the string variable thename with the numeric variable theage, the value is changed:
Strsql = "Insert into mytable (username, age) values ('" & thename & "'," & theage & ")"
Note the above formula. In short, replace the variable and complete the same string as the above after the connection.
6. Tips
One of the students found out a trick to change 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 & "')"-

Related Article

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.