【问题标题】:Query from ejabberd module with LIKE %%使用 LIKE %% 从 ejabberd 模块查询
【发布时间】:2016-01-25 10:07:20
【问题描述】:

我在 ejabberd 模块中有这行代码,它工作正常:

case catch ejabberd_odbc:sql_query(Server,["select COUNT(*) as total from spool where username='",IdUsername,"' AND xml LIKE '%message from%' AND xml LIKE '%chat%';"]) of
            {selected, [<<"total">>], [[Totale]]} ->
                Count = binary_to_list(Totale);
            _ -> Count = "0"
    end,

如果我转换这个:

LIKE '%chat%';

用这个:

LIKE '%type=\'chat\'%';

我收到一个错误,有什么想法吗?还是有其他方法只获取聊天消息?

【问题讨论】:

  • 没有可见错误,我总是得到 0,但是从 phpMyAdmin 相同的查询给了我三个记录!我认为问题是\'

标签: erlang ejabberd


【解决方案1】:

由于您在 Erlang 字符串中键入此内容,因此适用 Erlang escape sequences。特别是,\' 是一个转义序列,仅包含一个单引号 '。 (这在由单引号分隔的原子内部更有用。)

您可以在 Erlang shell 中尝试,发现 "\'""'" 是等价的:

1> "\'".
"'"
2> "\'" =:= "'".
true

要在字符串中包含实际的反斜杠,请使用另一个反斜杠对其进行转义:

"\\'"

在你的情况下,那将是:

LIKE '%type=\\'chat\\'%';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-25
    • 2012-07-27
    • 2010-12-29
    • 2012-12-16
    • 1970-01-01
    • 2016-01-12
    • 2012-09-24
    • 2018-06-21
    相关资源
    最近更新 更多