First we need to download MySQL ODBC Connector from here and create dsn.
-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.
Leave Comment