user1064227
user1064227

Reputation: 47

Do you need to use ecto to query a database?

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?

Upvotes: 0

Views: 1027

Answers (2)

brightball
brightball

Reputation: 933

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

Upvotes: 3

Sasha Fonseca
Sasha Fonseca

Reputation: 2311

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

Upvotes: 3

Related Questions