【问题标题】:Unicode insert of Russian into Oracle DB with Python 2.7使用 Python 2.7 将俄语的 Unicode 插入 Oracle DB
【发布时间】:2020-05-20 22:48:41
【问题描述】:

我遇到了 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()

结果:

【问题讨论】:

  • 永远不要使用+ 或其他字符串格式构造 SQL 字符串。它会导致注入漏洞。请参阅数据库适配器的文档以了解它是如何正确完成的。

标签: python oracle python-2.7 unicode cx-oracle


【解决方案1】:

请参阅 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 中的警告和链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多