我有以下UTF-8字节字符串我试图存储在MySQL表(utf8mb4)中,但是从mysql服务器获取失败。
...
db.Exec("SET NAMES 'utf8mb4'; SET CHARACTER SET utf8mb4;")
var badBytes = []byte{
34, 48, 34, 32, 47, 62, 66, 117, 121, 32, 105, 116, 32, 110, 111, 119, 32,
240, 159, 147, 149, 32, 60, 97, 32, 104, 114, 101, 102, 61, 34, 104, 116,
}
fmt.Println("UTF8 Valid", utf8.Valid(badBytes))
fmt.Println()
fmt.Println(string(badBytes))
fmt.Println()
res, err := db.Exec("INSERT INTO demo (body) VALUES (?)", string(badBytes))
if err != nil {
log.Fatal(err)
}
id, err := res.LastInsertId()
fmt.Println(id, err)
输出低于
UTF8 Valid true
="1" border="0" />Buy it now <a href="ht_tl
Error 1366: Incorrect string value: '\xF0\x9F\x93\x95 <...' for column 'body' at row 1
MySQL表和连接都是utf8mb4
:
CREATE TABLE `demo` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`body` text COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
答案 0 :(得分:0)
Like @Rico said我需要指定排序规则(utf8mb4_unicode_ci),因此go-mysql lib默认为 Traceback (most recent call last):
File "socket.py", line 1, in <module>
import socket
File "/home/linuxmint/Desktop/socket.py", line 9, in <module>
iphost()
File "/home/linuxmint/Desktop/socket.py", line 5, in iphost
packed = socket.inet_aton('192.168.1.15','127.0.0.1')
AttributeError: module 'socket' has no attribute 'inet_aton'
并导致我的4byte unicode符文出现问题(mysql&#39; s utf8仅支持最多3字节的unicode)。
utf8
有关详细信息,请参阅go-sql-driver readme。