SQLite3 区分大小写,就像在 python 2.7.12 中一样 [英] SQLite3 Case sensitive like in python 2.7.12
本文介绍了SQLite3 区分大小写,就像在 python 2.7.12 中一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它可能是双倍的,但是搜索Python sqlite casesensitive like"无法得到任何结果.
It may be a double, but with search "Python sqlite case sensitive like" couldn't get any results.
我在 python 2.7 上的 sqlite 3 模型中遇到了区分大小写的 LIKE 查询的问题.问题是这样的:LIKE with % 不区分大小写.
I've run into a problem with case sensitive LIKE query in sqlite 3 model on python 2.7. The problem looks like this: LIKE with % is case insensitive.
这里有一个例子:
import sqlite3
db = sqlite3.connect("test.db")
db.execute("""CREATE TABLE IF NOT EXISTS rec(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT );""")
db.commit()
for name in "ABC", "abc", "Abc", "aBC":
db.execute("INSERT INTO rec(name) VALUES(?);",(name,))
print "A%",db.execute("SELECT name FROM rec WHERE name LIKE ?;",("A%",)).fetchall()
print "a%",db.execute("SELECT name FROM rec WHERE name LIKE ?;",("a%",)).fetchall()
结果相当出人意料:
A% [(u'ABC',), (u'abc',), (u'Abc',), (u'aBC',)]
a% [(u'ABC',), (u'abc',), (u'Abc',), (u'aBC',)]
是否可以告诉 sqlite 区分大小写?
Is it possible to tell sqlite to be case sensitive?
推荐答案
你必须先执行 SQL 语句 "PRAGMA case_sensitive_like = on"
,然后它应该可以工作.
You have to execute the SQL statement "PRAGMA case_sensitive_like = on"
first, then it should work.
这篇关于SQLite3 区分大小写,就像在 python 2.7.12 中一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文