【问题标题】:sqlite full text search sqlite breaks when there is a dash in input当输入中有破折号时,sqlite 全文搜索 sqlite 中断
【发布时间】:2020-01-18 19:20:07
【问题描述】:

我的网络应用上有一个带有搜索栏的表单

@app.route('/search', methods=['POST','GET'])
def row_search():
    if request.method == 'POST':
        result = request.form
        print(result['search'])
        cursor.execute("""SELECT rowid,projeYili,projeAdi,ogretmenler,ogrenciler 
                    FROM projects
                    WHERE projects MATCH '{}'
                    """.format(result['search']))
    return render_template('index.html', projects=cursor.fetchall())

我已使用 MATCH 在名为项目的表中进行搜索,但如果我在搜索栏中输入特殊字符(?、-、。),则会出现错误

例如,如果我在搜索栏中输入 2018-2019,我会收到此错误 sqlite3.OperationalError: no such column: 2019 代码适用于没有特殊字符的文本

【问题讨论】:

  • 您使用的是哪个 FTS 扩展?
  • 您的第一步是不要将用户提供的值直接放在 sql 查询中;而是将其绑定到查询中的参数。 bobby-tables.com/python
  • @Shawn 我正在使用 FTS5 你是对的,我更改了我的代码以绑定查询和值,但它仍然给出相同的错误

标签: python sqlite flask full-text-search


【解决方案1】:

column filter 中,破折号表示不查看以下列。您作为搜索查询输入的内容可能会被解析,因此 -2019 被视为列过滤器。鉴于列过滤器标有冒号,我不确定它是如何发生的,但它会解释错误消息。我能够重现它,以后会尝试更多地挖掘。

但是,您可以通过将多词短语括在双引号中来搜索它,因此 "2018-2019""2018 2019" 都将匹配(默认标记器设置使用破折号作为单词分隔符)。但是,搜索 2018 2019 将匹配任何与这两个词出现的内容,无论它们出现在哪里,而不仅仅是相邻。

例子:

sqlite> CREATE VIRTUAL TABLE test USING fts5(body);
sqlite> INSERT INTO test VALUES ('in the years 2018-2019 something happened.');
sqlite> INSERT INTO test VALUES ('It was 2018 and then it was 2019');
sqlite> SELECT * FROM test WHERE test MATCH '2018-2019';
Error: no such column: 2019
sqlite> SELECT * FROM test WHERE test MATCH '"2018-2019"';
body                                      
------------------------------------------
in the years 2018-2019 something happened.
sqlite> SELECT * FROM test WHERE test MATCH '"2018 2019"';
body                                      
------------------------------------------
in the years 2018-2019 something happened.
sqlite> SELECT * FROM test WHERE test MATCH '2018 2019';
body                                      
------------------------------------------
in the years 2018-2019 something happened.
It was 2018 and then it was 2019          

有关查询语法的更多详细信息,请参阅the documentation

【讨论】:

    猜你喜欢
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多