如何在Elixir字符串中显示单个反斜杠

时间:2015-04-30 11:29:28

标签: elixir

有人可以告诉我如何在Elixir的SQL语句中添加单个反斜杠

iex(1)> sql = "select * from user limit 1 \G;" "select * from user limit 1 G;" iex(2)> sql = "select * from user limit 1 \\G;" "select * from user limit 1 \\G;"

我只需要' \ G'在我的sql语句中

$ elixir -v Elixir 1.1.0-dev

事实上,我想使用mariaex库,但我仍然无法使其工作

defmodule Customer do

    def main(args) do

        sql = "SELECT name FROM user limit 3 \\G;"

        {:ok, p} = Mariaex.Connection.start_link(username: "root", password: "password", database: "user")

        res = Mariaex.Connection.query(p, sql )

        IO.inspect res
    end
end

当我执行代码时,它告诉我我的语法错误是' \ G'

$ escript billutil
{:error,
 %Mariaex.Error{mariadb: %{code: 1064,
    message: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\\G' at line 1"},
  message: nil}}

我应该如何格式化字符串?

1 个答案:

答案 0 :(得分:8)

你的第二次尝试是正确的。您会在输出中看到两个反斜杠,因为它是一个经过检查的输出(打印为Elixir术语)。如果您尝试将该sql打印到控制台,您将看到一个反斜杠:

iex(1)> IO.puts("select * from user limit 1 \\G;")
select * from user limit 1 \G;