SQL语句中有关单引号、双引号和加号的问题

字符串数据是用单引号包在外面的,而+号只是用来连接这些字符串的. 数据库里的字段是整型的时候不要加单引号,是字符串的时候要加,其它类型根据实际情况来,双引号就是用来拼接字符串的,单引号是sql文的固有写法,因为你要动态的来拼接,涉及到变量,所以要用“+”来组合各个字符串片段。最终结果无非就是得出能在数据库查询分析器中执行的sql文。

        String sql = "insert into student values ( " + student.getId() + " ,' "
                + student.getUsername() + " ',  " + student.getAge() + " ,' "
                + student.getClassnumber()+" ')";

因为id和age是int型的所以不用加单引号,

你的Username在数据库中定义的是一个varchar型的,而对字符型进行条件查询的时候是要加 ' '号的: 
select   count(*)   from   student   where  username= 'aaa ' 
因此在后台写查询字符串的时候就必须这样写: 
string   sql   =   "select   count(*)   from   student   where  username= ' "+userName+ " ' " 
这样映射成的查询语句就是: 
select   count(*)   from  student  where  student= 'aaa '   了. 

原文地址:https://www.cnblogs.com/apolloren/p/8511452.html