[sqlalchemy] SQLAlchemy case insensitive like for unicode word

2011-01-20 Thread proft
Hello!

I have gtk application with sqlite db, contain russian words.

My model

code
class Patient(Base):
lastname = Column(Unicode)
/code

Search operation

code
patients = self.session.query(Patient)

lastname = unicode(self.lastname_entry.get_text())
if lastname:
 patients = patients.filter(Patient.lastname.like(u%%%s%% %
lastname))
/code

It perfectly work if i search as 'Ivanov', but didn't return results
if i search 'ivanov'. Note, i search russian words, not english.

In SQLite manager (firefox plugin) search query

code
SELECT * FROM patients WHERE lastname LIKE %ivanov%
/code

If i query db and look at value:

code
In [28]: p.lastname
Out[28]: u'\u0413\u0430\u043f\u0447\u0443\u043a'
/code

and than check it in query, generated by SQLAlchemy

code
In [29]: patients.filter(Patient.lastname.ilike(u%%%s%% %
l.decode('utf-8'))).count()

2011-01-20 21:20:30,950 INFO sqlalchemy.engine.base.Engine.0x...1250
SELECT count(1) AS count_1
FROM patients
WHERE lower(patients.lastname) LIKE lower(?)
2011-01-20 21:20:30,950 INFO sqlalchemy.engine.base.Engine.0x...1250
(u'%\u0433\u0430\u043f\u0447\u0443\u043a%',)
Out[29]: 0
/code

It is looked like lower function in SQLAlchemy didn't understood
cyrillic ...

Thanks!

PS: Sorry for my english)

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] SQLAlchemy case insensitive like for unicode word

2011-01-20 Thread Michael Bayer
That's SQLite's lower() function.   If you'd like to use Python's lower() 
function, you should call lower() on the string and use 
column.like(mystring.lower()).  But that won't do case-insensitive comparison 
since you need to call lower() on the database column in the statement.

So you really need to first get this statement to work with pysqlite, to figure 
out what collation issues may exist with sqlite3 or encoding issues with 
pysqlite:

import sqlite3

connection = sqlite3.connect(/path/to/your/db)

cursor = connection.cursor()
cursor.execute(SELECT * FROM patients WHERE lower(lastname) LIKE lower(?), 
['Ivanov'])
print cursor.fetchall()

SQLAlchemy doesn't look at the contents of your string at all with pysqlite, 
its a pass through.   



On Jan 20, 2011, at 4:15 PM, proft wrote:

 Hello!
 
 I have gtk application with sqlite db, contain russian words.
 
 My model
 
 code
 class Patient(Base):
lastname = Column(Unicode)
 /code
 
 Search operation
 
 code
 patients = self.session.query(Patient)
 
 lastname = unicode(self.lastname_entry.get_text())
 if lastname:
 patients = patients.filter(Patient.lastname.like(u%%%s%% %
 lastname))
 /code
 
 It perfectly work if i search as 'Ivanov', but didn't return results
 if i search 'ivanov'. Note, i search russian words, not english.
 
 In SQLite manager (firefox plugin) search query
 
 code
 SELECT * FROM patients WHERE lastname LIKE %ivanov%
 /code
 
 If i query db and look at value:
 
 code
 In [28]: p.lastname
 Out[28]: u'\u0413\u0430\u043f\u0447\u0443\u043a'
 /code
 
 and than check it in query, generated by SQLAlchemy
 
 code
 In [29]: patients.filter(Patient.lastname.ilike(u%%%s%% %
 l.decode('utf-8'))).count()
 
 2011-01-20 21:20:30,950 INFO sqlalchemy.engine.base.Engine.0x...1250
 SELECT count(1) AS count_1
 FROM patients
 WHERE lower(patients.lastname) LIKE lower(?)
 2011-01-20 21:20:30,950 INFO sqlalchemy.engine.base.Engine.0x...1250
 (u'%\u0433\u0430\u043f\u0447\u0443\u043a%',)
 Out[29]: 0
 /code
 
 It is looked like lower function in SQLAlchemy didn't understood
 cyrillic ...
 
 Thanks!
 
 PS: Sorry for my english)
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 To unsubscribe from this group, send email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/sqlalchemy?hl=en.
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.