articles

Home / DeveloperSection / Articles / Connect Mysql to Erlang odbc

Connect Mysql to Erlang odbc

Sunil Singh 8542 08-Sep-2015

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.

Updated 13-Dec-2017

Leave Comment

Comments

Liked By