在sql语句中经常会用到单引号双引号和&,下面以insert语句为例,select,update,delete语句都是一样的
1.插入字符串型
插入名字为张红的人,是字符串,所以insert语句当中名字两边要加单引号
strsql=“insert into mytable(username) values('张红') ”
如果名字变成了一个变量 thename,语句要写成:
strsql=“insert into mytable(username) values('“ & thename & ”') ”
注意所有的连接符前后需要有空格
如果插入的是两个字段:
strsql=“insert into mytable(username,type) values('张红',‘学生’) ”
strsql=“insert into mytable(username,type) values('“ & thename&”','“ & thetype & ”') ”
这时候插入了两个变量thename和thetype
2.插入数字型
插入数字型数据不用添加单引号
插入年龄18,语句:
strsql=“insert into mytable(age) values(18) ”
如果年龄变成一个the age的变量,
语句:strsql=“insert into mytable(age) values(“ & theage & ”) ”
这时候因为是数字所以不需要用单引号了
3.插入日期型数据,日期型数据和单引号类似,但是要将单引号换为#
Strsql="insert into mytabel (birthday) values (# 2019-5-2 #)"
将日期换为the table,strsql=“insert into mytable(birthday) values (#“ & thedate & ”#)”
4.插入布尔型,布尔型和数字类型类似,只不过是有两个值true 和false
strsql=“Insert into mytable(marry) values(True)”
如果换成布尔变量themarry
strsql=“Insert into mytable(birthday) values(“ & themarry & ”)”
总结:字符串变量需要用单引号,数字和布尔型变量不需要单引号,日期需要用#,access数据库中用单引号也可以
替换变量:要加双引号,双引号中间需要有连接符& &
在敲学生的时候,会看到下面这样的语句:
txtsql = "select * from user_info where user_id='" & Username & "'"
在这里username就是一个变量需要用双引号和连接符&