Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

ecto

stage2/ecto.livemd

ecto

Mix.install([
  {:ecto_sql, "~> 3.9"},
  {:ecto, "~> 3.9"},
  {:kino, "~> 0.10.0"},
  {:ecto_sqlite3, "~> 0.12"}
])

Section

defmodule Repo do
  use Ecto.Repo, adapter: Ecto.Adapters.SQLite3, otp_app: :nothing
end
defmodule User do
  use Ecto.Schema
  @primary_key false
  schema "users" do
    field(:name, :string)
    field(:age, :integer)
  end
end
case Ecto.Repo.all_running() do
  [_ | _] ->
    Repo.stop()

  _ ->
    []
end
Repo.start_link(database: "anti-aging.db")
[
  "DROP TABLE IF EXISTS users",
  "CREATE TABLE users (name STRING, age INT)"
]
|> Enum.map(&Ecto.Adapters.SQL.query!(Repo, &1, []))
users = [
  %{name: "๐Ÿ‘ถ", age: 0},
  %{name: "๐Ÿฆ–", age: 65_000_000},
  %{name: "๐Ÿค ", age: 92},
  %{name: "๐Ÿ‘ง", age: 12},
  %{name: "๐Ÿ‘ฉ๐Ÿป", age: 42},
  %{name: "๐Ÿฆ„", age: 101}
]
Repo.insert_all("users", users, [])
import Ecto.Query

min_age = 21
max_age = 55

query = from(u in User, where: u.age >= ^min_age and u.age <= ^max_age)

Repo.all(query)
|> Kino.DataTable.new()
query = from(u in User)

Repo.all(query)
|> Kino.DataTable.new()