Python:You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit byt

荆学民
2023-12-01

Python问题

You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.

发生情况

这个问题发生在我用Python的sqlite3操作数据库的时候发生,要插入的字符串中含有中文字符,做插入的时候就报当前这个错误。

解决

以utf8的编码格式进行解码转为unicode编码做插入

    def insert_tb_structured_fund(self, pack):
        sql, data = auto_insert_sql(pack, 'tb_structured_fund')
        data['m_stk_id_name'] = data.get('m_stk_id_name').decode('utf8')
        data['a_stk_id_name'] = data.get('a_stk_id_name').decode('utf8')
        data['b_stk_id_name'] = data.get('b_stk_id_name').decode('utf8')
        data['m_trace_index_name'] = data.get('m_trace_index_name').decode('utf8')
        self.db.execute_one(sql, data)
 类似资料:

相关阅读

相关文章

相关问答