[erlang-questions] gen_server and odbc problem about How to share a odbc connection
Andreas Hillqvist
andreas.hillqvist@REDACTED
Fri Mar 28 12:52:52 CET 2008
Hi.
The scope of an variable is within the function. So you will have to
pass a long the ODBC connection. This is made avalible by the gen
servers state variable.
It is recomended to use an record for the state. But if you only have
one or a very limited amount ov state varaible, you could use a tuple
or a list:
init([]) ->
process_flag(trap_exit, true),
{ok,Conn} = odbc:connect("DSN=flashpk;"
"UID=flashpk;"
"PWD=flashpk",
[{trace_driver, on}]),
{ok, Conn}.
handle_info({?PLLOGIN,Uid,Pwd}, Conn) ->
odbc:sql_query(Conn, "INSERT INTO customer (cno, password)"
"VALUES ('root', '123456')"),
{noreply, Conn}.
OR:
-record(state, {connection}).
init([]) ->
process_flag(trap_exit, true),
{ok,Conn} = odbc:connect("DSN=flashpk;"
"UID=flashpk;"
"PWD=flashpk",
[{trace_driver, on}]),
State = #state{connection = Conn},
{ok, State}.
handle_info({?PLLOGIN,Uid,Pwd}, #state{connection = Conn} = State) ->
odbc:sql_query(Conn,
"INSERT INTO customer (cno, password)"
"VALUES ('root', '123456')"),
{noreply, State}.
Kind regards
Andreas Hillqvist
2008/3/28, wenew zhang <wenewboy@REDACTED>:
> Dear All,
> Maybe another low-level question,but it's make me confused,
>
> init([]) ->
> process_flag(trap_exit, true),
> io:format("Authserver init with []~n"),
> % {ok,Bin}=file:consult(?AUTHCONF),
> % DDsn=element(2,lists:nth(1,Bin)),
> % Duser=element(2,lists:nth(2,Bin)),
> % Dpwd=element(2,lists:nth(3,Bin)),
> %%Ddatabase=element(2,lists:nth(4,Bin)),
> % DSNString="DSN="++DDsn++";UID="++Duser++";PWD="++Dpwd,
> % {ok,Conn}=odbc:connect(DSNString,[{trace_driver,on}]),%% read the
> odbc settings
> {ok,Conn}=odbc:connect("DSN=flashpk;UID=flashpk;PWD=flashpk",[{trace_driver,on}]),
> {ok,0}.
>
> handle_info({?PLLOGIN,Uid,Pwd},_State) ->
> io:format("PLLogin id:~w Uid:~w Pwd:~w~n",[?PLLOGIN,Uid,Pwd]),
> odbc:sql_query(Conn,"insert into customer(cno,password) values
> ('root','123456')"),
> {noreply, _State}.
>
> i want share the Conn ,but i get the unbound error,
> so How to share a odbc connection?
> or anybody have other solutions?
>
> Best Regards
>
> wenew zhang
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
More information about the erlang-questions
mailing list