5

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?

1 Answer 1

9

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;
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your reply, I actually want to execute a sql query using Mariaex. But I still not sure how to format the string to make it valid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.