今天尝试使用 cl-dbi 对 mysql 进行一些查询。但它失败并出现以下错误:
debugger invoked on a BABEL-ENCODINGS:INVALID-UTF8-STARTER-BYTE in thread
#<THREAD "main thread" RUNNING {B3E2151}>: Illegal :UTF-8 character starting at position 18.
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name): 0: [ABORT] Exit debugger, returning to top level.
((LABELS BABEL-ENCODINGS::UTF-8-DECODER :IN "/home/angelo/quicklisp/dists/quicklisp/software/cffi_0.14.0/src/strings.lisp")
#<unavailable lambda list>)
这是mysql字符集:
mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------+
| 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 (0.00 sec)
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 (0.00 sec)
在这里进行更多测试:
1. 我尝试根据post的建议将整个数据库转换为 utf-8 ,但仍然失败并出现同样的错误。
- 2015 年 8 月 3 日:这里我尝试查询当前 cl-dbi 连接中的 character_set 和 collation。结果如下:
**Query:**
(setf query (dbi:prepare *connection*
"SHOW VARIABLES LIKE 'character_set%';"))
(setf result (dbi:execute query))
(loop for row = (dbi:fetch result)
while row
do (format t "~A~%" row))
**Result:**
(Variable_name character_set_client Value latin1)
(Variable_name character_set_connection Value latin1)
(Variable_name character_set_database Value utf8)
(Variable_name character_set_filesystem Value binary)
(Variable_name character_set_results Value latin1)
(Variable_name character_set_server Value latin1)
(Variable_name character_set_system Value utf8)
(Variable_name character_sets_dir Value /usr/share/mysql/charsets/)
NIL
**Query:**
(setf query (dbi:prepare *connection*
"SHOW VARIABLES LIKE 'collation%';"))
(setf result (dbi:execute query))
(loop for row = (dbi:fetch result)
while row
do (format t "~A~%" row))
**Result:**
(Variable_name collation_connection Value latin1_swedish_ci)
(Variable_name collation_database Value utf8_unicode_ci)
(Variable_name collation_server Value latin1_swedish_ci)
NIL
正如“Fernando Borretti”所建议的,发送“SET names 'utf8';”后,查询成功完成,问题得到修复。以下是测试结果供参考:
**Sending:**
(setf query (dbi:prepare *connection* "SET names 'utf8';"))
(setf result (dbi:execute query))
(loop for row = (dbi:fetch result)
while row
do (format t "~A~%" row))
**Query:**
(setf query (dbi:prepare *connection*
"SHOW VARIABLES LIKE 'character_set%';"))
(setf result (dbi:execute query))
(loop for row = (dbi:fetch result)
while row
do (format t "~A~%" row))
**Result:**
(Variable_name character_set_client Value utf8)
(Variable_name character_set_connection Value utf8)
(Variable_name character_set_database Value utf8)
(Variable_name character_set_filesystem Value binary)
(Variable_name character_set_results Value utf8)
(Variable_name character_set_server Value latin1)
(Variable_name character_set_system Value utf8)
(Variable_name character_sets_dir Value /usr/share/mysql/charsets/)
NIL
**Query:**
(setf query (dbi:prepare *connection*
"SHOW VARIABLES LIKE 'collation%';"))
(setf result (dbi:execute query))
(loop for row = (dbi:fetch result)
while row
do (format t "~A~%" row))
**Result:**
(Variable_name collation_connection Value utf8_general_ci)
(Variable_name collation_database Value utf8_unicode_ci)
(Variable_name collation_server Value latin1_swedish_ci)
NIL