MySql超长自动截断实例详解
小伙伴问到一个问题,为啥在项目中调用插入或者更新语句时超长的字无法自动截断,而在navicat中直接执行是可以自动截断的?
如下
CREATE TABLE `p_app_station` ( `WX_APP_ID` varchar(20) NOT NULL, `APP_SECRET` varchar(33) DEFAULT NULL, `IS_BINDING` int(1) DEFAULT '0', `ACCOUNT_ID` int(13) DEFAULT NULL, `TOKEN` varchar(40) DEFAULT NULL, `BIND_URL` varchar(200) DEFAULT NULL, `WX_APP_NAME` varchar(50) DEFAULT NULL, `WX_APP_SID` varchar(50) DEFAULT NULL, `WX_NO` varchar(50) DEFAULT NULL, `CREATE_USER_ID` varchar(13) DEFAULT NULL, `UPDATE_DATE` datetime DEFAULT NULL, `CREATE_DATE` datetime DEFAULT NULL, `UPDATE_USER_ID` varchar(13) DEFAULT NULL, `STATION_TYPE` int(1) unsigned zerofill DEFAULT NULL COMMENT '标记类型(试用版:0,会员版:1,定制版:2)', `ACTIVE_DATE` datetime DEFAULT NULL COMMENT '使用时间截止', `APP_MODULE_ID` varchar(60) DEFAULT NULL COMMENT '推送模版消息ID', PRIMARY KEY (`WX_APP_ID`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
insert into p_app_station(wx_app_id) values('12121312312312啊啊啊啊啊aassasdasd');select * from p_app_station where wx_app_id like '12121312312312%'; |
很明显varchar(20) 不足以容纳12121312312312啊啊啊啊啊aassasdasd
查询结果如下
确实自动截断了,但是在项目中执行同样的sql发现并非如此,反而报错。
Data truncated for column '%s' at row %ld |
考虑到是同一个数据库,不存在模式不同,那么可能性应该出现在jdbcDriver上。
查看jdbc源码
private void setupServerForTruncationChecks() throws SQLException { if (getJdbcCompliantTruncation()) { if (versionMeetsMinimum(5, 0, 2)) { String currentSqlMode = this.serverVariables.get("sql_mode"); boolean strictTransTablesIsSet = StringUtils.indexOfIgnoreCase(currentSqlMode, "STRICT_TRANS_TABLES") != -1; if (currentSqlMode == null || currentSqlMode.length() == 0 || !strictTransTablesIsSet) { StringBuilder commandBuf = new StringBuilder("SET sql_mode='"); if (currentSqlMode != null && currentSqlMode.length() > 0) { commandBuf.append(currentSqlMode); commandBuf.append(","); } commandBuf.append("STRICT_TRANS_TABLES'"); execSQL(null, commandBuf.toString(), -1, null, DEFAULT_RESULT_SET_TYPE, DEFAULT_RESULT_SET_CONCURRENCY, false, this.database, null, false); setJdbcCompliantTruncation(false); // server's handling this for us now } else if (strictTransTablesIsSet) { // We didn't set it, but someone did, so we piggy back on it setJdbcCompliantTruncation(false); // server's handling this for us now } } }} |
新闻热点
疑难解答