SQLite 自定义函数作为匹配字符串

SQLite custom functions as match string

我创建了一个 SQLite 函数,它接受一个字符串和 return 另一个字符串,然后我可以使用 return 值作为匹配字符串。代码是 here

除了单引号外,它工作得很好。在这种情况下,它无法匹配任何行,但是如果我直接使用 returned 字符串,它可以匹配。有人知道这里有什么问题吗?

sqlite> select simple_query('''');
"''"
sqlite> select '    ', simple_highlight(t1, 0, '[', ']') from t1 where x match simple_query('''');
sqlite> select '    ', simple_highlight(t1, 0, '[', ']') from t1 where x match '"''"';
    |@English &special _characters."[']bacon-&and[']-eggs%

完整示例here

这个问题终于在sqlite-forum中得到了解答,我想post在这里说明原因。

原因是 SQLite 会尝试为我们转义字符串,我们可以验证打开引用模式后,如您所见,我们的 return 值将从 "'" 转义为 "' '" 由 SQLite。这意味着我们不需要在函数中转义单引号。

sqlite> select simple_query('''');
"'"
sqlite> select simple_query('"');
""""
sqlite> .mode quote
sqlite> select simple_query('"');
'""""'
sqlite> select simple_query('''');
'"''"'