dwyl / learn-phoenix

:fire: Phoenix is the web framework without compromise on speed, reliability or maintainability! Don't settle for less. :rocket:
649 stars 45 forks source link

How to run a "raw" SQL query using Ecto.Adapters.SQL.query #33

Open nelsonic opened 7 years ago

nelsonic commented 7 years ago

On page 119 of Programming Phoenix there's an example for how to write a "direct" SQL query:

Ecto.Adapters.SQL.query(Rumbl.Repo, "SELECT, power($1, $2)", [2, 10])

This does not SELECT any data from the database it (is meant to) merely calculate 2^10 which should return 1024 ... instead we see: image

It's not a DB/connection error because other queries work! e.g: image

So... I'm left scratching my head as to how to run an SQL query using Ecto ... has anyone else attempted to do this?

sntran commented 7 years ago

You probably figured it out long time ago, but you have an extra comma after "SELECT".

Another note is that you need to use "SELECT" to retrieve the result.

Lastly, the result is not "1024", but rather, %{rows: [{1024}], num_rows: 1}. Your code will need to convert to desired format.

samhstn commented 6 years ago

as @sntran said SELECT, power($1, $2) is invalid sql, removing the first comma fixes the problem