Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Python with MySql unicode problems

I need to call MySQL stored procedure from my python script. As one of parameters I'm passing a unicode string (Russian language), but I get an error;

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)

My script:

 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()

I've read that setting charset='utf8' shuld resolve this problem, but when I use string:

self.db=MySQLdb.connect("localhost", "usr", "pass", "dbName", charset='utf8')

This gives me another error;

UnicodeEncodeError: 'utf-8' codec can't encode character '\udcd1' in position 20: surrogates not allowed

Also I've trying to set parametr use_unicode=True, that's not working.

Answer*

Draft saved
Draft discarded
Cancel

default

AltStyle によって変換されたページ (->オリジナル) /