Reputation: 18705
I'm creating a method which takes a name as an argument and returns a row from Sqlite3
database. When trying to use non-diacritic characters, it works.
But if I try to pass a string
which contains diacritic characters, it raises:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 4: ordinal not in range(128)
When I try to print the same name to the console, it works.
# -*- coding: utf-8 -*-
import sqlite3
conn = sqlite3.connect('data.db')
cur = conn.cursor()
def get_subject_by_name(name): # TODO: APROXIMATE SEARCH
result = cur.execute("""SELECT nazov,ICO,pravna_forma,sidlo,predmet_cinnosti,den_zapisu,url_orsr FROM companies WHERE nazov LIKE ?""",('{}%'.format(name),)).fetchall()
print result
get_subject_by_name(u"systém")
do you know where the problem could be?
Upvotes: 0
Views: 120
Reputation: 177461
It comes from your format
:
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> '{}%'.format(u'syst\xe9m')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 4: ordinal not in range(128)
You're trying to format a Unicode string into a byte string and Python 2 tries to coerce the Unicode string into a byte string using the default ascii
codec.
Use Unicode strings everywhere:
>>> u'{}%'.format(u'syst\xe9m')
u'syst\xe9m%'
Including your SELECT:
result = cur.execute(u"""SELECT nazov,ICO,pravna_forma,sidlo,predmet_cinnosti,den_zapisu,url_orsr FROM companies WHERE nazov LIKE ?""",(u'{}%'.format(name),)).fetchall()
Upvotes: 1