forked from jkvor/emysql
-
Notifications
You must be signed in to change notification settings - Fork 208
/
a_hello.erl
64 lines (56 loc) · 1.76 KB
/
a_hello.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
% ------------------------------------------------------------------------
% Hello World: Minimal sample of Emysql
% H. Diedrich <hd2010@eonblast.com> - Eonblast http://www.eonblast.com
% 11 Jun 2010
% ------------------------------------------------------------------------
%
% This sample inserts 'Hello World!' into a mysql table and reads it back.
%
% If you have trouble, try the simpler and more sturdy 'b_raw' sample.
%
% ------------------------------------------------------------------------
%
% Create local mysql database:
%
% $ mysql ...
% 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';
% mysql> quit
%
% On *nix build and run using the batch a_hello in folder samples/:
%
% $ ./a_hello
%
% - or -
%
% Make emysql and start this sample directly, along these lines:
%
% $ cd Emysql
% $ make
% $ cd samples
% $ erlc a_hello.erl
% $ erl -pa ../ebin -s a_hello run -s init stop -noshell
%
% ------------------------------------------------------------------------
%
% Expected output: [[<<"Hello World!">>]]
%
% ------------------------------------------------------------------------
-module(a_hello).
-export([run/0]).
run() ->
crypto:start(),
application:start(emysql),
emysql:add_pool(hello_pool, [{size,1},
{user,"hello_username"},
{password,"hello_password"},
{database,"hello_database"},
{encoding,utf8}]),
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.