Elixir Ecto simple SQL Query
Posted by Pete McBreen 21 May 2019 at 06:00
Demo after the previous post, showing just executing SQL
Interactive Elixir (1.8.1) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> alias Ora.Repo Ora.Repo iex(2)> result = Ecto.Adapters.SQL.query!(Repo, "select * from scott.emp where empno = :1 ", [7369]) 13:34:54.892 [debug] QUERY OK db=16.0ms select * from scott.emp where empno = :1 [7369] %{ columns: ["EMPNO", "ENAME", "JOB", "MGR", "HIREDATE", "SAL", "COMM", "DEPTNO"], num_rows: 1, rows: [ [7369, "SMITH", "CLERK", 7902, ~N[1980-12-17 00:00:00], 800.0, nil, 20] ] } iex(3)>
Note. Can also use Ecto.Adapters.SQL.query which will then return the usual tuple
iex(3)> result = Ecto.Adapters.SQL.query(Repo, "select * from scott.emp where empno = :1 ", [7369]) 13:38:05.739 [debug] QUERY OK db=16.0ms select * from scott.emp where empno = :1 [7369] {:ok, %{ columns: ["EMPNO", "ENAME", "JOB", "MGR", "HIREDATE", "SAL", "COMM", "DEPTNO"], num_rows: 1, rows: [ [7369, "SMITH", "CLERK", 7902, ~N[1980-12-17 00:00:00], 800.0, nil, 20] ] }} iex(4)>
Need to set MIX_ENV to dev/test/prod to switch between environments.