我需要從我的python腳本調用MySQL存儲過程。作爲參數之一,我傳遞了一個unicode字符串(俄語),但我得到一個錯誤;Python與MySql unicode問題
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)
我的腳本:
self.db=MySQLdb.connect("localhost", "usr", "pass", "dbName")
self.cursor=self.db.cursor()
args=("какой-то текст") #this is string in russian
self.cursor.callproc('pr_MyProc', args)
self.cursor.execute('SELECT @_pr_MyProc_2') #getting result from sp
result=self.cursor.fetchone()
self.db.commit()
我讀過設置charset='utf8'
shuld解決此問題,但是當我使用的字符串:
self.db=MySQLdb.connect("localhost", "usr", "pass", "dbName", charset='utf8')
這給了我另一個錯誤;
UnicodeEncodeError: 'utf-8' codec can't encode character '\udcd1' in position 20: surrogates not allowed
另外我試圖設置參數use_unicode=True
,這是行不通的。
它的工作原理(u「какой-тотекст」)'? –
@DanielRoseman不,我也試過了。 – Gleb
是這個蟒蛇2或3? –