码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Mysql入库不了表情符号怎么办


    目录

    前言

    异常信息:

    主要原因是

     解决办法


    前言

    异常信息:

    ### The error occurred while setting parameters ### SQL: INSERT INTO news_info ( id,  title, content, author, summary, sensitives, scraping_time, level, news_type, content_text, original_columns, content_words ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )

    ### Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x91\x89' for column 'author' at row 1 ;

    uncategorized SQLException; SQL state [HY000];

    error code [1366];

    Incorrect string value: '\xF0\x9F\x91\x89' for column 'author' at row 1;

    nested exception is java.sql.SQLException: Incorrect string value: '\xF0\x9F\x91\x89' for column 'author' at row 1 at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:89)

    主要原因是

    author  含有表情符号,表情是4字符,如果Mysql建库建表的时候没有声明是4字节,那么是在insert的时候会报错的!

     解决办法

    需要在消费入库的时候进行author转换成正常的二进制,二字符的汉字,也就是剔除四字节的表情;

    String  author=  ContentUtils.filterOffUtf8Mb(newsInfo.getAuthor());
    newsInfo.setAuthor(author);

    主要得工具类代码

    1. /**
    2. * 主要功能是做表情包剔除工具类
    3. * @param text
    4. * @return
    5. */
    6. public static String filterOffUtf8Mb(String text) {
    7. if (StringUtils.isBlank(text)) {
    8. return text;
    9. }
    10. String result = text;
    11. try {
    12. byte[] bytes = text.getBytes(UTF_CHARACTOR);
    13. ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    14. int i = 0;
    15. while (i < bytes.length) {
    16. short b = bytes[i];
    17. if (b > 0) {
    18. buffer.put(bytes[i++]);
    19. continue;
    20. }
    21. // 去掉符号位
    22. b += 256;
    23. if (((b >> 5) ^ 0x06) == 0) {
    24. buffer.put(bytes, i, 2);
    25. i += 2;
    26. } else if (((b >> 4) ^ 0x0E) == 0) {
    27. buffer.put(bytes, i, 3);
    28. i += 3;
    29. } else if (((b >> 3) ^ 0x1E) == 0) {
    30. i += 4;
    31. } else if (((b >> 2) ^ 0xBE) == 0) {
    32. i += 5;
    33. } else {
    34. i += 6;
    35. }
    36. }
    37. buffer.flip();
    38. result = new String(buffer.array(), UTF_CHARACTOR);
    39. } catch (Exception ex) {
    40. log.error("内容过滤4字节字符出现错误" + ex.getMessage());
    41. }
    42. return result;
    43. }

  • 相关阅读:
    vue里面如何动态添加class类名和style样式
    HTML转义字符
    qt 多语言版本 QLinguist使用方法
    利用Python实现矢量逐个图斑裁剪栅格,形成图斑对应的栅格文件
    2022杭电多校七 1007-Weighted Beautiful Tree(树形DP)
    Linux ARM平台开发系列讲解(摄像头V4L2子系统) 2.12.2 摄像头V4L2出图框架介绍
    每日刷题记录 (十三)
    数据分析9
    数学建模学习笔记:层次分析法(AHP)
    机器学习笔记之最优化理论与方法(六)无约束优化问题——最优性条件
  • 原文地址:https://blog.csdn.net/m0_59252007/article/details/125477784
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号