gpt4 book ai didi

python - pyodbc 无法正确处理 unicode 数据

转载 作者:行者123 更新时间:2023-11-29 19:15:04 26 4
gpt4 key购买 nike

我确实使用pyodbc成功连接了MySQL数据库,并且它可以很好地处理ascii编码的数据,但是当我打印使用unicode(utf8)编码的数据时,它引发了错误:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)

所以我检查了行中的字符串:

>>>row[3]
'\xe7\xae\xa1\xe7\x90\u2020\xe5\u2018\u02dc'

我找到了instructions about unicode in pyodbc github wiki

These databases tend to use a single encoding and do not differentiate between "SQL_CHAR" and "SQL_WCHAR". Therefore you must configure them to encode Unicode data as UTF-8 and to decode both C buffer types using UTF-8.

# Python 3.x
cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
cnxn.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
cnxn.setencoding(encoding='utf-8')

If you are using MySQL, you can add the character set to the connection string, but I'm not sure if this is necessary.

# MySQL
cstring = 'DSN=mydsn;CharSet=utf8'
cnxn = pyodbc.connect(cstring)

我按照上面的方法做了,但没有什么不同。我的代码流畅

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyodbc
import configparser
class ServerDBDAO():
def __init__(self):
''' Establish connection to SQL'''
# Read config
self.cf = configparser.ConfigParser()
self.cf.read("./Config/server.ini")
driver = self.cf.get('Database', 'Driver')
server = self.cf.get('Database', 'Server')
database = self.cf.get('Database', 'Database')
uid = self.cf.get('Database', 'UID')
pwd = self.cf.get('Database', 'PWD')

# Connect database
connString = 'DRIVER=%s;SERVER=%s;DATABASE=%s;UID=%s;PWD=%s;CharSet=utf8'%(driver, server, database, uid, pwd)
'''Successfully connected database with this
self.conn = pyodbc.connect('DRIVER=/usr/lib/x86_64-linux-gnu/odbc/libmyodbc.so;SERVER=localhost;DATABASE=xxx;UID=root;PWD=xxxxxx'))
'''
self.conn = pyodbc.connect(connString,unicode_results=True)
self.conn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
self.conn.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
self.conn.setencoding(encoding='utf-8')
self.cursor = self.conn.cursor()

def __del__(self):
self.conn.commit()
self.conn.close()

测试代码:

from ServerDBDAO import ServerDBDAO
dbdao = ServerDBDAO()
row_employee = cursor.execute('select id, name, email from Employee;').fetchone()
print(row_employee.name)

最佳答案

我也遇到了同样的问题。除了使用这些之外:

cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
cnxn.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
cnxn.setencoding(encoding='utf-8')

添加此内容解决了我的问题:

cnxn.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-32le')

关于python - pyodbc 无法正确处理 unicode 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42780705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com