2点半解决一个数据库问题,特此记录!
环境:
jdk8 + mysql5.7
小错一:
Column count doesn‘t match value count at row 1
说的是列名不匹配。
示例是 insert into user(colum1,colum2,colum3)
但是写成了values是(字段1,字段2)这种信息。
大错二:
项目环境搭建好后,出现中文等录入不了mysql数据库,如下:
解决 Incorrect string value: for column ‘name’ at row 1 问题
插入数据报错:
Incorrect string value: ‘\xE5\x8D\x97\xE5\xA4\xA9…’ for column ‘name’ at row 1
首先想到的是mysql对应表的字符编码不对。
INSERT INTO `sys_user` VALUES ('ff80808176d667560176d66bab950001', 'ADMIN', '2021-01-06', -1, 'ADMIN', '2021-01-08 17:15:13', 'string', 1, 'string', '值班用户', 0, '张三1', '35aaafa878438f9f85e6db7ed42e0511', '15627099511', 'admin', NULL, NULL, NULL);
INSERT INTO `sys_user` (id,created_by,created_date,is_del,last_modified_by,last_modified_date,
remark,version,ip_address,jobs,lock_ip,nickname,password,phone,username,gender_id,role_id)
VALUES ('ff80808176d667560176d66bab950001', 'ADMIN', '2021-01-06', -1, 'ADMIN',
'2021-01-08 17:15:13', 'string', 1, 'string', NULL,
0, '张三', '35aaafa878438f9f85e6db7ed42e0511',
'15627099511', 'admin', NULL, NULL);
原因:
MySQL的字符集错误,不识别中文,导致报错。
解决方法:
1、先查看默认字符集:
mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set
可以看到mysql的字符集是latin1(ISO_8859_1),需要手动设置一下。
2、修改默认字符集
mysql> SET character_set_client = utf8 ;
mysql> SET character_set_connection = utf8 ;
mysql> SET character_set_database = utf8 ;
mysql> SET character_set_results = utf8 ;
mysql> SET character_set_server = utf8 ;
mysql> SET collation_connection = utf8 ;
mysql> SET collation_database = utf8 ;
mysql> SET collation_server = utf8 ;
修改完后,重启mysql的服务。
service mysql restart
ok,系统可以正常录入中文了。
感谢网友,参考链接:http://www.nowamagic.net/librarys/veda/detail/138