On Thu, Jul 07, 2016 at 11:23:21PM -0500, Art Zemon wrote:
> Hello,
>
> I have a column in a table that contains Hebrew text. I need to do a query
> with a LIKE clause on the Hebrew text. The query works if I execute the SQL
> directly, either in a SQL window of phpMyAdmin or in a command line mysql
> client. But I cannot get it to work within Django.
>
> I tried Bible.objects.filter(hebrew_text__contains('דֶּשֶׁא') and I get
> nothing back.
Here, one thing you might want to try is to use django-debug-toolbar's
debugsqlshell, run the query inside that, and take a look at the
actual SQL query generated for that; maybe it will reveal some oddity.
> I tried a raw query in a custom manager:
>
> class BibleManager(models.Manager):
> def contains_hebrew_word(self, word='דֶּשֶׁא'):
> sql = """select sortkey, book, chapter, verse, hebrew_text
> from bible_bible
> where hebrew_text like '%%%s%%' """
> raw_query_set = self.raw(sql, [word])
> result_list = []
> for b in raw_query_set:
> result_list.append(b)
> return result_list
>
> but I get this error:
>
> django.db.utils.ProgrammingError: (1064, "You have an error in your SQL
> syntax; check the manual that corresponds to your MySQL server version for
> the right syntax to use near 'דֶּשֶׁא'%'' at line 3")
I may be wrong, but it seems to me that you're using wrong quoting in
that raw query; I think it should be::
select sortkey, book, chapter, verse, hebrew_text
from bible_bible
where hebrew_text like '%%' || %s || '%%'
The DBAPI will already take care of the proper quoting for the string
argument, you just need to properly concatenate it with percent signs
on both sides.
Good luck,
Michal