今天尝试使用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,但它仍然以相同的错误失败。
**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
As&#34; Fernando Borretti&#34;建议,在发送&#34; SET名称&#39; utf8&#39;;&#34;后,查询成功完成并且问题已修复。 以下是测试结果供参考:
**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