当前位置:  开发笔记 > 编程语言 > 正文

查询sqlite到LIKE但整个单词

如何解决《查询sqlite到LIKE但整个单词》经验,为你挑选了1个好方法。

我有两个表:TableA:id,StringA TableB:id,StringB

从TableA中获取所有行后,我希望匹配尽可能接近TableB中的单词.我正在尝试做这样的事情:

c.execute('SELECT id, StringB FROM TableB WHERE StringB LIKE "'+stringa+'%" COLLATE NOCASE')
foundrows=c.fetchall()
if (len(foundrows)>0):
    print 'Hmm. Which one...'
    for foundrow in foundrows:    
        print 'looking for:'+stringa+'  found:'+ foundrow[1]

哪个给我这样的东西:

Hmm. Which one...
looking for:goo  found:good
looking for:goo  found:good 1
looking for:goo  found:good 2
looking for:goo  found:good 1 dk
looking for:goo  found:good 2 dk
looking for:goo  found:Good Friday
looking for:goo  found:goose
looking for:goo  found:Good Friday 1
looking for:goo  found:good sport
looking for:goo  found:good job
looking for:goo  found:good morning
looking for:goo  found:good night
looking for:goo  found:goodbye 1
looking for:goo  found:goodbye e
looking for:goo  found:goodbye

我真正想要的是'SELECT id,StringB FROM TableB WHERE StringB CONTAINS WORD"'+ stringa +'"

什么是实现"包含Word"的最佳方式?



1> Blorgbeard..:

构建一个这样的where子句:

where (' ' || StringB || ' ') LIKE '% stringa %'

周围的空格StringB确保您在字符串的开头和结尾处捕获单词.


但它将与中间带有“ stringa”的记录匹配。
推荐阅读
echo7111436
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有