daveaztig14

daveaztig14

How to connect simple elxiir app to MySQL database and execute query?

Hi,

I have built a simple elxiir app which receives request like:

curl "http://localhost:8085/pass" -H Content-Type: application/json; charset=utf-8’ -d '{"ticket": 1, "ver": "1", "os": "1"}'

and the output is:

ticket: 1, ver: 0.0.1.0_develop, os: ios

SQL Query = select * from database where ticket= 1 AND ver= 1 AND os= 1

(for the process or steps on how i did it please refer to How to use curl as input in elixir script? - #4 by NobbZ)

Anyway, I have a MySQL in remote host (sample ip: 10.10.11.11), a database named fortesting and a table named testingone.

My question is how connect my elixir app to my MySQL database in remote host (ip: 10.10.11.11)? and run the query select * from fortesting where ticket= 1 AND ver= 1 AND os= 1? using for example simple_server.exs and not the interactive elixir?

I’ve tried this: Executing raw SQL queries in Elixir | AmberBit Sp. z o. o. but did not work for me.

Any ideas, suggestions, links and help are welcome. Thank you.

Most Liked

NobbZ

NobbZ

What is the exact error you see? What behaviour did you observe, which did you expect?

stefanluptak

stefanluptak

Since you’re using MySQL, you probably want to use MyXQL.

GitHub: GitHub - elixir-ecto/myxql: MySQL 5.5+ driver for Elixir · GitHub
Docs: MyXQL — MyXQL v0.9.0

joaoevangelista

joaoevangelista

:wave:
It is possible to use raw drivers? Yes.
Suppose you want to use on a simple, not phoenix project
creating a project using mix new my_app --sup will create the folders and the implementation of Application (the main entry point) which starts the supervision tree.

defmodule MyApp.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      # Starts a worker by calling: MyApp.Worker.start_link(arg)
      # {MyApp.Worker, arg}
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Now the driver is just a “Worker” and we can start under the supervisor.

  • Add {:myxql, "~> 0.2.0"} to your dependencies and run mix deps.get, this is a new driver made for newer versions of mysql
  • Supports MySQL 5.7.10+, 8.0, and MariaDB 10.3
  • Now under the MyApp.Application.start/2 function we have the children list, there we will add the initialization for the driver.
children = [
  {MyXQL, hostname: "10.10.11.11", username: "username", password: "password", database: "fortesting", name: :mydb}
]

Note that we add a key name to the options with value :mydb that is our identifier to use the same driver process in the application without the need to start/stop when a request comes.

Now on your controller action or any where you need, even on iex -S mix :

{:ok, result} = MyXQL.query(:mydb, "select * from database where ticket = ? and ver = ? and os = ?", [ticket_param, ver_param, os_param])

Performing the query with ? on the string allows the driver do the sanatization, preventing SQL Injection.

And thats it. For more infor on what you get from MyXQL.query/3 you can see the docs and the github repo

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement