Skip to content

Instantly share code, notes, and snippets.

@jamesbee
Last active August 29, 2015 14:16
Show Gist options
  • Save jamesbee/2be415394aff81dc8103 to your computer and use it in GitHub Desktop.
Save jamesbee/2be415394aff81dc8103 to your computer and use it in GitHub Desktop.
Elixir connect to mysql example
defmodule Conn do
def test do
stat = "select * from users limit 10;"
:emysql.add_pool :demo_pool, [
size: 1,
user: 'root',
password: 'root',
database: 'demo',
encoding: :utf8 # you will need this
]
res = :emysql.execute :demo_pool, stat
rows = :emysql.as_proplist res
# from keyword list to elixir map
Enum.map rows, &Enum.into(&1, %{})
end
end
defmodule SomeProject.Mixfile do
use Mix.Project
##
# bla, bla, bla
##
# Add crypto, emysql to application start link.
def application do
[applications: [:crypto, :emysql]]
end
# Add emysql dependencies
defp deps do
[{:emysql, github: "Eonblast/Emysql", ref: "HEAD"}]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment