查询避免Unknown column ‘xxx’ in ‘where clause’
来源:互联网 发布:linux 看一个目录大小 编辑:程序博客网 时间:2024/04/29 18:23
但凡写过sql语句的人估计都曾经碰到过类似于Unknown column ‘xxx’ in ‘where clause’的问题。 单从字面理解,我们很容易得出
列名不存在的结论,但是,很多时候起始并不是由于列名出错造成的。而是由于拼凑sql语句时对字符类型数据没有用引号引起来造成的。
比如如下一个自定义查询,我们很多时候都写成如下:
String [] prerequisites = {” caseCode = ” + acase.getCaseCode(),” caseId <> ” + acase.getCaseId(),” isDelete <> 1 “};
执行这个查询后,就出现了如下异常:
WARN [org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:77)] - SQL Error: 1054, SQLState:
42S22
ERROR [org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:78)] - Unknown column
‘RS345345′ in ‘where clause’
ERROR [com.bester.zjj.dao.BaseDAOImpl.findByPrerequisite(BaseDAOImpl.java:246)] - get failed
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is
org.hibernate.exception.SQLGrammarException……
我们再来看看打印出来的语句sql语句
INFO [com.bester.zjj.dao.BaseDAOImpl.findByPrerequisite(BaseDAOImpl.java:243)] - from Case where 1=1 and caseCode =
EHC9008 and caseId <> null and isDelete <> 1
很显然,EHC9008是个varchar类型,而在sql中,数字可以不用”好,但是varchar类型必须包含在引号内的。
好的,我们把sql修改下,看看执行效果,修改后的sql如下:
String [] prerequisites = {” caseCode = ‘”+acase.getCaseCode()+ “‘“,” caseId <> ” + acase.getCaseId(),” isDelete <> 1 “};
然后看看执行后打印的sql语句:
INFO [com.bester.zjj.dao.BaseDAOImpl.findByPrerequisite(BaseDAOImpl.java:243)] - from Case where 1=1 and caseCode =
‘EHC9008′ and caseId <> null and isDelete <> 1
ok,这个时候正常了。问题是小,我就曾经多次碰到过,希望引以为戒。
- 查询避免Unknown column ‘xxx’ in ‘where clause’
- 查询避免Unknown column ‘xxx’ in ‘where clause’
- 查询避免Unknown column ‘xxx’ in ‘where clause’
- 数据库 —— 查询避免 Unknown column ‘xxx’ in ‘where clause’ 错误
- mybatis查询报错 Unknown column 'xxx' in 'where clause'
- Unknown column 'xxx' in 'where clause'错误
- JDBC出现 "Unknown column 'xxx' in 'where clause' "
- TP 框架解决Unknown column 'XXX' in 'where clause' 问题
- msql查询中报错 Unknown column '黄色水果' in 'where clause'
- Unknown column 'rownum' in 'where clause'解决方案
- Unknown column '安妮宝贝' in 'where clause'
- Unknown column '??????' in 'where clause'问题
- Unknown column 'ORGANIZATION_BANK' in 'where clause'
- 中文乱码 unknown column in 'where clause'
- MySql报错原因分析Unknown column ‘xxx’ in ‘where clause’
- Column not found: 1054 Unknown column 'tl10709' in 'where clause'
- c#Bug Unknown column “张三” in 'where clause'问题解决
- mysql Error:1052 Column 'xxx' in where clause is ambiguous
- 谷歌,爱我别走
- PHP生成中文拼音
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)
- 关于OSGI的概念
- jquery和json使用代码
- 查询避免Unknown column ‘xxx’ in ‘where clause’
- js实现右下角消息提示功能
- 反省一下
- C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
- 无题
- 学习BMC Remedy笔记1
- OSSIM相关PDF
- C&GCC&GDB使用详解与举例
- 用VPN联网玩游戏(在内外与外网玩局域网游戏)