使用 Python 2.7 将俄语的 Unicode 插入 Oracle DB

Unicode insert of Russian into Oracle DB with Python 2.7

我的 unicode 俄语单词有问题。我有 python 2.7.

# -*- coding: utf-8 -*-
from __future__ import print_function
import cx_Oracle
import csv
import sys
# sys.setdefaultencoding() does not exist, here!
reload(sys)  # Reload does the trick!
sys.setdefaultencoding('UTF8')
teh2 = ['Col1','Col2']
va = [(108, u"русский")]
for i in va:
    cur.execute("INSERT INTO Mytable("+ teh2[0] + "," + teh2[1] +") VALUES('"+ str(i[0]) + "', '"+ str(i[1]) + "')")
con.commit()
con.close()

结果:

请参阅 cx_Oracle 文档中的 Characters Sets and National Language Support (NLS)。您可能想使用类似以下内容的方式进行连接:

import cx_Oracle
connection = cx_Oracle.connect(connectString, encoding="UTF-8", nencoding="UTF-8")

数据库的字符集也有一定的作用,因为插入的数据将映射到该字符集。

关于字符串连接 - 如评论中所述,永远不要这样做。请参阅 SQL Queries 中的警告和链接。