Erlang/OTP Forums

Author Message

<  Advanced Erlang/OTP  ~  [Windows, R13B, ODBC,gen_server]

phicarre
Posted: Tue Jun 16, 2009 2:46 pm Reply with quote
User Joined: 23 Mar 2009 Posts: 14
Hello,
I try to interface a mySQL database with ODBC by using a gen_server module.
When I call sql_query, I jump in the terminate callback with the following message (I simplified):

function_clause, [{odbc,sql_query,[<0,223,0>,<<"hello">>,infinity]] ...

in Init, I do:
Code:

process_flag(trap_exit,true),
odbc:start(),
ConnectString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDB; User=root;Password=myPwd;Option=3;",
{ok,Ref}= odbc:connect(ConnectString, [{scrollable_cursors,off}]),
{ok, [Ref]},


in handle_cast, I do:
Code:

handle_cast(Msg,Ref)->
Result = sql_query(Ref,Msg),     ***** hangs here *****
{noreply,Ref}.


in terminate, I do
Code:

terminate(Reason,Ref)->
odbc:disconnect(Conn),
odbc:stop(),
io:format("Reason: ~s~n",[Reason]),
ok.


I have a method:
Code:

sql_query(Msg)->
 gen_server:cast(?MODULE,Msg).


I was expected an error such "wrong SQL syntax" !
Any idea ? I think the error is in my gen_server but I don't see it ...
I tried with a string syntaxically correct but I had the same message.
View user's profile Send private message
Cy6erBr4in
Posted: Tue Jun 16, 2009 6:52 pm Reply with quote
Joined: 16 Oct 2008 Posts: 9
Hej!

It would be better to see full version of your code.

I don`t see a call of the odbc:sql_query function in the showed code at all.

Code:

function_clause, [{odbc,sql_query,[<0,223,0>,<<"hello">>,infinity]]


this error means that you tried to call function which can`t match arguments you passed.

for example:

Code:

some_fun({test, Data}, Arg2) ->
<some code here>.

another_fun() ->
    some_fun({test1, "Test"}, true).


if you try this code, you will get an error with the same reason ("function_clause").

Good luck!
View user's profile Send private message
phicarre
Posted: Tue Jun 16, 2009 9:52 pm Reply with quote
User Joined: 23 Mar 2009 Posts: 14
I didn't copy correctly the code ...

Result = odbc:sql_query(Ref,Msg), ***** hangs here *****

sql_query(Ref,SQLquery)-> ResultTuple | [ResultTuple] | {error,Reason} according to the doc.
View user's profile Send private message
phicarre
Posted: Tue Jun 16, 2009 10:07 pm Reply with quote
User Joined: 23 Mar 2009 Posts: 14
Here is the full code:

Code:

-module(database).
-behaviour(gen_server).

%% External exports
-export([start_link/1]).
-export([mysql_query/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).


%% ====================================================================
%% Method
%% ====================================================================

mysql_query(Querystring)->
   gen_server:cast(?MODULE,Querystring).

%% ====================================================================
%% Server functions
%% ====================================================================

start_link(Name)->
   io:format("--> Démarrage du serveur Database ~s~n",[Name]),
   gen_server:start_link({local,?MODULE},?MODULE,[],[]).
%% --------------------------------------------------------------------
%% Function: init/1
%% Description: Initiates the server
%% Returns: {ok, State}          |
%%          {ok, State, Timeout} |
%%          ignore               |
%%          {stop, Reason}
%% --------------------------------------------------------------------
init([]) ->
   process_flag(trap_exit,true),
   case odbc:start() of
      X when X =:= ok;X =:= {error,{already_started,odbc}}  ->
         ConnectString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDB; User=root;Password=myPwd;Option=3;",
         case odbc:connect(ConnectString, [{scrollable_cursors,off}]) of
         {ok,Conn} ->
            io:format("ODBC Starting on Pid ~p~n",[Conn]),
            {ok, Conn};               
         {error,Reason} ->
            io:format("ODBC Connection error; ~s~n",[Reason]),
            {ok,odbc_error}         % ???
         end;
      Other ->
         io:format("ODBC Connection error; ~s~n",[Other]),
         {stop,odbc_not_started}      % ???
   end.

%% --------------------------------------------------------------------
%% Function: handle_call/3
%% Description: Handling call messages
%% Returns: {reply, Reply, State}          |
%%          {reply, Reply, State, Timeout} |
%%          {noreply, State}               |
%%          {noreply, State, Timeout}      |
%%          {stop, Reason, Reply, State}   | (terminate/2 is called)
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_call(Request, From, State) ->
    Reply = ok,
   
    {reply, Reply, State}.

%% --------------------------------------------------------------------
%% Function: handle_cast/2
%% Description: Handling cast messages
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_cast(Msg, Conn) ->
   io:format("cast on Pid ~p~n",[Conn]),
   Qres = odbc:sql_query(Conn,Msg),
   {noreply, State}.

%% --------------------------------------------------------------------
%% Function: handle_info/2
%% Description: Handling all non call/cast messages
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_info(Info, Conn) ->
    {noreply, Conn}.

%% --------------------------------------------------------------------
%% Function: terminate/2
%% Description: Shutdown the server
%% Returns: any (ignored by gen_server)
%% --------------------------------------------------------------------
terminate(Reason, Conn) ->
   odbc:disconnect(Conn),
   odbc:stop(),
   io:format("Terminated: reason = ~s~n",[Reason]),
    ok.

%% --------------------------------------------------------------------
%% Func: code_change/3
%% Purpose: Convert process state when code is changed
%% Returns: {ok, NewState}
%% --------------------------------------------------------------------
code_change(OldVsn, State, Extra) ->
    {ok, State}.

%% --------------------------------------------------------------------
%%% Internal functions
%% --------------------------------------------------------------------
 
View user's profile Send private message
phicarre
Posted: Wed Jun 17, 2009 1:25 pm Reply with quote
User Joined: 23 Mar 2009 Posts: 14
Forget my question, I found myself
The correct syntax is

Result=odbc:sql_query(Ref,[Msg])

The documentation is wrong ...
View user's profile Send private message
Cy6erBr4in
Posted: Wed Jun 17, 2009 4:51 pm Reply with quote
Joined: 16 Oct 2008 Posts: 9
Good work! Smile

So, I suppose it`s good reason to send a "patch" for the documentation!

/Regards,
Alexander
View user's profile Send private message
wuji
Posted: Sat Sep 15, 2012 7:06 am Reply with quote
User Joined: 10 Aug 2012 Posts: 654
me in the house."She said that wanting to be accepted for who who cheap jordan shoes who she is drove her to come out on television that year
Jerry Falwell, Pat Robertson and other members of the evangelical community who who cheap designer *beep* who said they were disappointed. Some called her "Ellen Degenerate.""You know Ellen
such a good person," her mother said. "Now, I don't want to to cheap polo shirts to get weepy, I don't, but she's so good that she shouldn't
all this directed at her."Today Betty DeGeneres fights for gay rights. She She [h1]designer replica *beep*[/h1] She is devoted and always tolerant.Bill Clinton Boosts President Obama - So
George W. Bush for Mitt Romney?2 Prior Presidents Take Dramatically Different Approaches Approaches cheap Ralph Lauren Polo Approaches to 2012 CampaignBy JOEL SIEGELMay 4, 2012 — He was once
most dominant figure in American politics, his approval rating an astounding 90 90 cheap designer *beep* 90 percent. But four years after leaving office, former President George W.
View user's profile Send private message
mbtshoes88
Posted: Wed Sep 19, 2012 8:50 am Reply with quote
User Joined: 18 Sep 2012 Posts: 30
Go backstage at DSquared2 Fall 2012 with designers Dean & Dan Caten Dsquared2 Shoes and top models Cara Delevingne and Bette Franke, Frida Aasen, Daphne Groenveld, Dsquared2 Sale Online Arizona Muse.Get the gorgeous beauty look that Dsquared2 Online makeup Dsquared2 Belts artist Charlotte Tilbury and hairstylist Sam McKnight created for the Dsquared2 Clothing show that was inspired by the 60′s and the icon of today, Cheap Dsquared2 Online Lana del Rey. Makeup artist Charlotte Tilbury gave the girls a gorgeous supermodel brow, and used Dsquared2 Clothing Sale thick black liner to create Cheap Dsquared2 a wing. She poured on the Dsquared2 Denim mascara and added two layers of lashes. Charlotte finished off the look using MAC Spice Lip liner and Dsquared2 Jeans FleshPot lipstick to give the girls that sexy Lana del Rey pout. “It’s all Dsquared2 Shop Online about the feline eye, the pouty lip and the Dsquared2 Outlet Sale lash“.DSquared2 fashion shows Cheap Dsquared2 Outlet are always one of the best shows of Milan Fashion Week. Dean & Dan always Dsquared2 Sale stage a show that is entertaining and full of beautiful clothes that Dsquared2 Store Online you actually want to own and wear. The concept behind their Fall Dsquared2 Online Sale 2012 fashion show is a 60′s ‘Back to School’ high school prom. As the boys told us backstage, Dsquared2 Outlet Online “these are rich kids with a bad Dsquared2 Outlet attitude“.Hear what top model Cara Delevingne and Anna Dello Russo, Vogue Japan Editor-at-Large think about this collection.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum