Skip to content

Instantly share code, notes, and snippets.

@seven1240
Created January 22, 2011 07:15
Show Gist options
  • Save seven1240/790941 to your computer and use it in GitHub Desktop.
Save seven1240/790941 to your computer and use it in GitHub Desktop.
% Seven changed to illustrate a problem of stored procedure
% run3() turns that return #result_packet and #ok_packet alternatively.
%
% ------------------------------------------------------------------------
% Hello World: Minimal sample usage of emysql
% H. Diedrich <hd2010@eonblast.com> - Eonblast http://www.eonblast.com
% 11 Jun 2010
% ------------------------------------------------------------------------
%
% Instructions:
%
% Create local mysql database:
% mysql> create database hello_database;
% mysql> use hello_database;
% mysql> create table hello_table (hello_text char(20));
% mysql> grant all privileges on hello_database.* to hello_username@localhost identified by 'hello_password';
%
% On *nix build and run using batch a_hello in folder samples:
% $ ./a_hello
%
% - or -
%
% Make emysql and start this sample manually along these lines:
% $ cd ..
% $ make
% $ cd samples
% $ erlc a_hello.erl
% $ erl -pa ../ebin -s a_hello run -s init stop -noshell
%
% ------------------------------------------------------------------------
% Questions, mail Henning <hd2010@eonblast.com>, welcome.
% ------------------------------------------------------------------------
-module(a_hello).
-export([run/0]).
run() ->
crypto:start(),
application:start(emysql),
emysql:add_pool(hello_pool, 1,
"hello_username", "hello_password", "localhost", 3306,
"hello_database", utf8),
run3(4),
% run2(4),
% emysql:execute(hello_pool,
% <<"INSERT INTO hello_table SET hello_text = 'Hello World!'">>),
%
% { _, _, _, Result, _ } = emysql:execute(hello_pool,
% <<"select hello_text from hello_table">>),
%
% io:format("~n~p~n", [Result]),
ok.
% create procedure sp_hello() begin select * from hello_table; end;
run2(0) ->
ok;
run2(N) ->
Result = emysql:execute(hello_pool,
<<"select hello_text from hello_table">>),
io:format("~p~n", [Result]),
run2(N-1).
run3(0) ->
ok;
run3(N) ->
io:format("~n"),
Result = emysql:execute(hello_pool,
<<"call sp_hello();">>),
io:format("~p~n", [Result]),
run3(N-1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment