2011-10-29 35 views
0

我有兩個使用utf-8排序規則的表。 我使用Django從一個表中讀取行並將其插入到其它如下:Django模型的編碼錯誤

cursor.execute("select * from Table1") 
results = dictfetchall(cursor) 
for row in results: 
    table2_row = Table2(name=row["Name"]) 
    table2_row.save() 

的問題是,我有時會收到錯誤:

'ascii' codec can't encode characters in position 17-18: ordinal not in range(128)

,這是什麼原因錯誤?兩個表都使用utf-8歸類。

+1

你寫的'#coding? –

+0

將完整的曲線粘貼回去。根據問題中的代碼判斷,你不需要'#coding:utf-8'。 –

回答

0

嘗試做這些工作:

1-添加# encoding: UTF-8在最前面你的模塊
2 - 定義字符集時創建數據庫類似這樣的片段:
Create Database DB_NAME CHARACTER SET = UTF8;
3-嘗試分配row['name']這樣的:在一開始UTF-8`:
name = row['name'].encode('utf8')