client.execute(sql)报错UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 220-221: invalid continuation byte
时间: 2023-11-27 11:48:14 浏览: 136
Maven报错: Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.0.2:resources
该错误通常是由于Oracle数据库中的数据编码与Python默认编码不同导致的。可以尝试在连接Oracle数据库时指定编码格式,例如:
```python
import cx_Oracle
dsn = cx_Oracle.makedsn(host='localhost', port=1521, sid='orcl1')
connection = cx_Oracle.connect(user='scott', password='tiger', dsn=dsn, encoding='UTF-8')
cursor = connection.cursor()
# 执行SQL语句
sql = "SELECT * FROM emp_test1"
cursor.execute(sql)
# 获取查询结果
result = cursor.fetchall()
# 关闭连接
cursor.close()
connection.close()
```
如果指定编码格式后仍然出现错误,可以尝试将查询结果转换为Python默认编码格式,例如:
```python
import cx_Oracle
dsn = cx_Oracle.makedsn(host='localhost', port=1521, sid='orcl1')
connection = cx_Oracle.connect(user='scott', password='tiger', dsn=dsn, encoding='UTF-8')
cursor = connection.cursor()
# 执行SQL语句
sql = "SELECT * FROM emp_test1"
cursor.execute(sql)
# 获取查询结果并转换编码格式
result = cursor.fetchall()
result = [[str(cell.encode('latin1'), 'utf-8') if isinstance(cell, bytes) else cell for cell in row] for row in result]
# 关闭连接
cursor.close()
connection.close()
```
阅读全文