| Author |
Message |
|
| phicarre |
Posted: Tue Jun 16, 2009 2:46 pm |
|
|
|
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. |
|
|
| Back to top |
|
| Cy6erBr4in |
Posted: Tue Jun 16, 2009 6:52 pm |
|
|
|
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! |
|
|
| Back to top |
|
| phicarre |
Posted: Tue Jun 16, 2009 9:52 pm |
|
|
|
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. |
|
|
| Back to top |
|
| phicarre |
Posted: Tue Jun 16, 2009 10:07 pm |
|
|
|
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
%% --------------------------------------------------------------------
|
|
|
| Back to top |
|
| phicarre |
Posted: Wed Jun 17, 2009 1:25 pm |
|
|
|
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 ... |
|
|
| Back to top |
|
| Cy6erBr4in |
Posted: Wed Jun 17, 2009 4:51 pm |
|
|
|
Joined: 16 Oct 2008
Posts: 9
|
Good work!
So, I suppose it`s good reason to send a "patch" for the documentation!
/Regards,
Alexander |
|
|
| Back to top |
|
| wuji |
Posted: Sat Sep 15, 2012 7:06 am |
|
|
|
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. |
|
|
| Back to top |
|
| mbtshoes88 |
Posted: Wed Sep 19, 2012 8:50 am |
|
|
|
User
Joined: 18 Sep 2012
Posts: 30
|
|
| Back to top |
|
|
|
All times are GMT
|
|
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
|
|
|