我在将unicode插入Oracle模式时遇到问题,我认为数据库是Oracle 11g实例,但目前还不确定.我在OS X 10.6.8上使用python 2.6.1(这是python的系统版本),并使用从sourceforge.net下载的cx-Oracle驱动程序模块5.1,构建并安装到virtualenv 1.6.1实例网站包可见.我的脚本如下
import cx_Oracle
connection = cx_Oracle.connect(
"<name>/<password>@<host>/<service-name>"
)
cursor = connection.cursor()
result = cursor.execute(u"create table UNICODE_TEST (id NUMBER(6), text NCLOB not NULL)")
raw_text = open("test.txt",'r').read()
if isinstance(raw_text,str):
raw_text = raw_text.decode("utf_8")
statement = u"insert into UNICODE_TEST (id, text) values (1,'%s')" % raw_text
result = cursor.execute(statement)
Run Code Online (Sandbox Code Playgroud)
我创建一个连接,创建游标,执行一个语句来创建一个测试表,其中包含NUMBER和NCLOB类型的id和text字段.我打开一个文件,其中包含我所知道的以UTF-8编码的文本,将字符串解码为unicode.在unicode字符串中创建插入语句并执行该语句,结果是此错误.
Traceback (most recent call last):
File "unicode-test.py", line 19, in <module>
result = cursor.execute(statement)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 170: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
在将语句插入Oracle模式之前,有些东西试图将我的语句编码为ASCII.所以我开始寻找更好地了解cx-Oracle如何处理unicode,并在我从sourceforge.net下载的cx-Oracle源代码的HISTORY.txt中找到它
从5.0.4到5.1的更改 …
我正在尝试为一些Django json_view视图编写一些单元测试,而我在将json_string传递给视图时遇到了麻烦.我昨天发布了一个相关的问题,关于将json字符串传递给JS的Django视图,问题是在我的JS中我只是传递json字符串,我需要传递字符串作为对象的属性,因为我如果没有这样做,则字符串被视为生成的查询字典的关键字.我再次遇到类似的问题,除了这次它是Django View的Django单元测试.这是我的代码的简化版本,它产生相同的结果.
class MyTestCase(TestCase):
def setUp(self):
self.u = User.objects.create_user('test','test','test')
self.u.is_active = True
self.u.save()
self.client.login(username='test',password='test')
def test_create_object_from_form(self):
"""Test the creation of the Instance from the form data."""
import json
json_string json.dumps({'resource':{'type':'book','author':'John Doe'}})
print(json_string)
response = self.client.post(reverse('ajax_view'),
{'form':json_string},'json')
self.assetNotContains(response,'error')
Run Code Online (Sandbox Code Playgroud)
并且视图看起来像这样
@json_view
def ajax_view(request):
"""Process the incoming form data."""
if request.method == 'POST':
print(request.POST)
form_data = json.loads(request.POST['form'])
resource_data = form_data['resource']
form = MyUserForm(resource_data)
if form.is_valid():
...
Run Code Online (Sandbox Code Playgroud)
以下是运行测试时两个打印语句产生的内容.json_string是
{"resource": {"type": "book", "author": "John Doe"}}
Run Code Online (Sandbox Code Playgroud)
和查询字典看起来像
<QueryDict: {u'{\'form\': \'{"resource": {"type": "book", "author": "John Doe"}}\'}': …Run Code Online (Sandbox Code Playgroud) 我遇到了一个令人讨厌的小问题,通过SQLAlchemy使用服务名称连接到Oracle模式.这是我的代码作为脚本.(出于安全原因,尖括号之间的项目是实际值的占位符)
from sqlalchemy import create_engine
if __name__ == "__main__":
engine = create_engine("oracle+cx_oracle://<username>:<password>@<host>/devdb")
result = engine.execute("create table test_table (id NUMBER(6), name VARCHAR2(15) not NULL)")
result = engine.execute("drop table test_table")
Run Code Online (Sandbox Code Playgroud)
其中'devdb'是服务名称而不是SID.运行此脚本的结果是堆栈跟踪.
(oracle-test)[1]jgoodell@jgoodell-MBP:python$ python example.py
Traceback (most recent call last):
File "example.py", line 8, in <module>
result = engine.execute("create table test_table (id NUMBER(6), name VARCHAR2(15) not NULL)")
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1621, in execute
connection = self.contextual_connect(close_with_result=True)
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1669, in contextual_connect
self.pool.connect(),
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/pool.py", line 272, in connect
return _ConnectionFairy(self).checkout()
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/pool.py", …Run Code Online (Sandbox Code Playgroud) oracle ×2
python ×2
ajax ×1
cx-oracle ×1
django ×1
json ×1
sqlalchemy ×1
unicode ×1
unit-testing ×1