Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Display a single backslash in Elixir string

Tags:

elixir

Can somebody tell me how to add a single backslash in the SQL statement in Elixir

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;"

I just need '\G' in my sql statement

$ elixir -v Elixir 1.1.0-dev

In fact, I want to use the mariaex library, but I still cannot make it work

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

When I execute the code it tell me that I have a syntax error around '\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}}

How should I format the string please?

like image 202
王志軍 Avatar asked Feb 11 '23 06:02

王志軍


1 Answers

Your second attempt is correct. You see two backslashes in the output, since it's an inspected output (printed as Elixir term). If you try printing that sql to console, you'll see one backslash:

iex(1)> IO.puts("select * from user limit 1 \\G;")
select * from user limit 1 \G;
like image 194
sasajuric Avatar answered Feb 23 '23 15:02

sasajuric