cl-dbi从sbcl查询mysql,错误 - Illegal:从位置18开始的UTF-8字符

时间:2015-07-31 11:48:25

标签: mysql lisp common-lisp clsql

今天尝试使用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,但它仍然以相同的错误失败。

  1. 2015年8月3日:我在这里尝试查询当前cl-dbi连接中的character_set和collat​​ion。结果如下:
  2. **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
    

1 个答案:

答案 0 :(得分:1)

发送SET NAMES utf8会有帮助吗?我从回复this评论中得到了这个想法。