Home > DeveloperSection > Beginner > Connect Mysql to Erlang odbc

Connect Mysql to Erlang odbc


erlang MYSQL  OTP  erlang 
Ratings:
0 Comment(s)
 1653  View(s)
Rate this:

Connect Mysql to Erlang odbc


First we need  to download MySQL ODBC Connector from here and create dsn.
i have created a connection_setting.hrl file  with  following connection  string-
-define(CONNECTIONSTRING, 

"dsn=mysql_dsn;server=localhost;database=testdb;user=root;password=test;").



%% @author sunil
%% @doc @todo Add description to odbc_mysql.

-include("connection_setting.hrl").
-module(odbc_mysql).

-export([mysql_connect/0,check_record_exist/2]).
mysql_connect()->
     application:stop(odbc),
case application:start(odbc) of
    ok->
        {ok, Ref}=odbc:connect(?CONNECTIONSTRING,[]),
        Ref;
   
     {error,Reason}->
      Reason
end.

check_record_exist(Sql,Where_cond)->
     Ref=mysql_connect(),
    case odbc:select_count(Ref, string:concat(Sql,Where_cond)) of
  {ok,Rows}->
         io:format("check_record_exist success~p",[odbc:disconnect(Ref)]),
         Rows;
  {error,Reason}->
      io:format("check_record_exist error~p~n~p",[odbc:disconnect(Ref),Reason])
end.

Don't want to miss updates? Please click the below button!

Follow MindStick