如何使用 Lucee 将 UTF-8 数据插入 MySQL?

How do I insert UTF-8 data into MySQL using Lucee?

我正在尝试使用 Lucee 将 UTF-8 字符插入 MySQL table,但没有成功。

我是运行的代码如下...

<cfset textValue = ' Person Raising Hand'>
<cfdump var="#textValue#">
<cfquery name="insert">
INSERT INTO TEST_UTF8 (TestText)
VALUES ('#textValue#');
</cfquery>

转储工作正常并正常显示,但插入 returns 出现以下错误...

"Incorrect string value: '\xF0\x9F\x99\x8B P...' for column 'TestText' at row 1"

我试过通过 phpmyadmin 插入相同的字符串,结果很好,表明 MySQL 配置没问题。

那个 4 字节的表情符号需要 MySQL 的 utf8mb4utf8不会做。对于外界,utf8mb4 被称为 UTF-8(带破折号)。

table 中的列需要说 CHARACTER SET utf8mb4 连接也需要说:

?useUnicode=yes&characterEncoding=UTF-8添加到JDBC URL

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8" %>

compileJava.options.encoding = 'UTF-8'

<form method="post" action="/your/url/" accept-charset="UTF-8">

要将 4 字节 UTF8 与 Connector/J 配合使用,请将 MySQL 服务器配置为 character_set_server=utf8mb4。只要 characterEncoding 尚未在连接字符串中设置,Connector/J 就会使用该设置。这相当于自动检测字符集。

好的,有了更多的帮助和阅读,我需要将 "character_set_server=utf8mb4" 添加到位于“/etc/mysql/my.conf”的 MySQL 配置文件中。看起来这是由旧版本的连接器引起的问题。

来自 MySQL 文档(感谢 CfSimplicity 引导我访问该页面)...

For Connector/J 8.0.12 and earlier: In order to use the utf8mb4 character set for the connection, the server MUST be configured with character_set_server=utf8mb4; if that is not the case, when UTF-8 is used for characterEncoding in the connection string, it will map to the MySQL character set name utf8, which is an alias for utf8mb3.