0

Came across this link regarding use of Ecto. Elixir ecto connect to an existing DB . Schemas are already created in a database so why declare here? We use stmt like Ecto.Adapters.SQL.query when using odbc/jdbc/odac drivers and are less complicated and straightforward.

So where do give the stmt: Ecto.Adapters.SQL.query(YourRepo, "SELECT $1", [1])?
Is it in web/models/abc.ex? And how do you get back the results? Can you pl mention some of benefits you get from Ecto that use of direct SQL does'nt provide?

Community
  • 1
  • 1
user1064227
  • 47
  • 2
  • 7
  • There is also the path of using Erlang's ODBC libraries to query a DB as well. But I think @sashafonseca laid out several good reasons to use Ecto instead. – Onorio Catenacci May 24 '16 at 19:54

2 Answers2

3

Could you please edit your question with a more elaborate and detailed description and content, please?

About the benefits:

  • Ecto provides a very good and expressive language to query databases
  • Queries are sanitized and protected against SQL Injection
  • Provides easy to build validations to ensure the inserted data obeys the rules you want
  • Makes it easier to handle the data directly in Elixir
  • Handles Database connections and allows to spawn more workers as needed
Sasha Fonseca
  • 2,257
  • 23
  • 41
  • Thanks for the answers. Since a schema will already be created in database, one can use structured query language directly, using Ecto.Adapters.SQL.query(), to fetch data. – user1064227 May 24 '16 at 12:17
  • Yes but there are easier ways through Ecto. I recommend reading some guides or the Programming Phoenix book to better understand and see how easy and useful Ecto is. – Sasha Fonseca May 24 '16 at 12:33
  • Thanks for the answers. Just learning Elixir. Since a schema is already created in a database, I think you can use structured query language directly, (using Ecto.Adapters.SQL.query() here), to fetch data. Or use stored procedures. Use bind variables to prevent SQL injection. For validation , use both client and serverside. Perhaps Ecto could be useful in handling the data fetched(?) and to spawn more workers. Can you pl explain how Ecto makes easier to handle data directly? What about connection pool? Do you use Ecto to create a connection pool? – user1064227 May 24 '16 at 12:37
3

It sounds like Ecto might not be the best fit for what you're trying to do with it. Ecto is great for new projects where you can start with it from scratch using migrations and building the schemas up in the system as you go. For hooking up to an existing database, it can get a little tedious as you're trying to remap all of the existing items though.

Luckily, you're not tied to Ecto if it doesn't meet your needs. You might want to give something like Moebius a try since it seems to take exactly the approach that you're looking for.

Moebius is not an ORM. There are no mappings, no schemas, no migrations; only queries and data. We embrace PostgreSQL as much as possible, surfacing the goodness so you be a hero.

https://github.com/robconery/moebius

brightball
  • 923
  • 13
  • 11