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()