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

KinoDB

livebooks/kino/kino_db.livemd

KinoDB

Mix.install([
  {:kino_db, "~> 0.2"},
  {:postgrex, "~> 0.17"}
])

Connect to DB

opts = [
  hostname: "postgres_for_livebook",
  port: 5432,
  username: "postgres",
  password: System.fetch_env!("LB_DB_PASSWORD"),
  database: "postgres"
]

{:ok, conn} = Kino.start_child({Postgrex, opts})

Execute Query

result =
  Postgrex.query!(
    conn,
    """
    CREATE TABLE
      sample
    (
      id integer,
      name varchar(20)
    )
    """,
    []
  )
result3 =
  Postgrex.query!(
    conn,
    """
    INSERT INTO
      sample
    (
      id,
      name
    )
    VALUES
    (
      1,
      'John'
    )
    RETURNING
      id
    """,
    []
  )
result2 =
  Postgrex.query!(
    conn,
    """
    SELECT
      *
    FROM
      sample
    """,
    []
  )

SmartCell to Code

opts = [
  hostname: "postgres_for_livebook",
  port: 5432,
  username: "postgres",
  password: "postgres",
  database: "postgres"
]

{:ok, conn2} = Kino.start_child({Postgrex, opts})
result4 = Postgrex.query!(conn2, "SELECT * FROM sample", [])