如何使用python和sqlite3对非ASCII进行不区分大小写的查询?

时间:2010-11-08 14:04:09

标签: python django unicode sqlite

有一些明显的提示和资源,但我找不到任何直接的例子。

http://www.sqlite.org/faq.html#q18表示可以覆盖NOCASE排序规则等等,低位和高位函数。我已经在Django测试用例中这样做了,但在我的测试中,实际上并没有调用它们。

from django.test import TestCase

来自django.contrib.sites.models导入网站 来自basic.blog.models导入帖子,设置

class CaseInsenstiveTest(TestCase):
    def setUp(self):
        self.site = Site.objects.create()
        self.settings = Settings.objects.create(site=self.site)
        Settings.get_current = classmethod(lambda cls: self.settings)

    def testLike(self):
        from django.db import connection

        Post.objects.all() # Sets up the connection

        def like(x, y, z=None):
            """Y LIKE X [ESCAPE Z]"""

            assert x.startswith('%') and x.endswith('%')
            like.called = True
            print "like(%r, %r, %r)" % (x, y, z)

            return x[1:-1].lower() in y.lower()
        like.called = False

        def lower(s):
            print "lower(%r)" % (s,)
            return s.lower()

        def upper(s):
            print "upper(%r)" % (s,)
            return s.upper()

        connection.connection.create_function('lower', 1, lower)
        connection.connection.create_function('upper', 1, upper)
        connection.connection.create_function('like', 3, like)

        def NOCASE(a, b):
            print "NOCASE(%r, %r)" % (a, b)
            return cmp(a.lower(), b.lower())
        connection.connection.create_collation('NOCASE', NOCASE)

        Post.objects.create(slug='foo', title='Foo')
        Post.objects.filter(title__icontains='foo')

它似乎不是一个已注册的函数或实际调用的排序规则。谁能指出出了什么问题?

注意:我知道类似的功能还不正确。我只想弄清楚什么叫做什么,所以我知道我需要覆盖什么以及如何。

1 个答案:

答案 0 :(得分:2)

似乎在django之外工作正常:

所以也许你有一个django问题?您确定表字段是使用NOCASE归类创建的吗?

import sqlite3

def NOCASE(a, b):
    print 'comparing %r with %r...' % (a, b)
    return cmp(a.lower(), b.lower())

con = sqlite3.connect('')

cur = con.cursor()
cur.execute('CREATE TABLE foo (id INTEGER, text VARCHAR collate NOCASE)')
cur.executemany('INSERT INTO foo (id, text) VALUES (?, ?)', [
    (1, u'test'), (2, u'TEST'), (3, u'uest'), (4, u'UEST')])
con.commit()

con.create_collation('NOCASE', NOCASE)

cur = con.cursor()
cur.execute('SELECT * FROM foo ORDER BY text ASC')
print cur.fetchall()

输出:

comparing 'test' with 'TEST'...
comparing 'test' with 'uest'...
comparing 'TEST' with 'uest'...
comparing 'TEST' with 'UEST'...
comparing 'uest' with 'UEST'...
[(1, u'test'), (2, u'TEST'), (3, u'uest'), (4, u'UEST')]

同样,使用定义的函数可以正常工作(相同的数据集)

def my_lower(text):
    print "I'm lowering %r myself" % (text,)
    return text.lower()

con.create_function('lower', 1, my_lower)
cur.execute('SELECT lower(text) FROM foo')

输出:

I'm lowering u'test' myself
I'm lowering u'TEST' myself
I'm lowering u'uest' myself
I'm lowering u'UEST' myself
[(u'test',), (u'test',), (u'uest',), (u'uest',)]

类似地,对于LIKE操作,如果您打算以2参数形式(X LIKE Y)和3参数形式(X LIKE Y ESCAPE Z)注册该函数,则必须支持两种形式:

def my_like(a, b, escape=None):
    print 'checking if %r matches %r' % (a, b)
    return b.lower().startswith(a[0].lower())

con.create_function('like', 2, my_like) # X LIKE Y
con.create_function('like', 3, my_like) # X LIKE Y ESCAPE Z
cur.execute('SELECT * FROM foo WHERE text LIKE ?', (u't%',))

产生输出:

checking if u't%' matches u'test'
checking if u't%' matches u'TEST'
checking if u't%' matches u'uest'
checking if u't%' matches u'UEST'
[(1, u'test'), (2, u'TEST')]