forked from jkvor/emysql
-
Notifications
You must be signed in to change notification settings - Fork 208
/
c_rows_as_records.erl
98 lines (79 loc) · 2.94 KB
/
c_rows_as_records.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
% ------------------------------------------------------------------------
% Emysql: sample for accessing rows as records
% H. Diedrich <hd2010@eonblast.com> - Eonblast http://www.eonblast.com
% 12 Jun 2010
% ------------------------------------------------------------------------
%
% This sample does the same as the previous samples but uses a record
% to access the result row.
%
% ------------------------------------------------------------------------
%
% Create local mysql database (or re-use the one made for a_hello):
%
% $ 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/:
%
% $ ./c_rows_as_records
%
% - or -
%
% Make emysql and start this sample directly, along these lines:
%
% $ cd Emysql
% $ make
% $ cd samples
% $ erlc c_rows_as_records.erl
% $ erl -pa ../ebin -s c_rows_as_records run -s init stop -noshell
%
% ------------------------------------------------------------------------
%
% Expected Output:
%
% ...
% ... many PROGRESS REPORT lines ...
% ...
%
% Record: <<"Hello World!">>
%
% ------------------------------------------------------------------------
-module(c_rows_as_records).
-export([run/0]).
%% -----------------------------------------------------------------------
%% Record Definition: 1
%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-record(hello_record, {hello_text}).
%% -----------------------------------------------------------------------
run() ->
application:start(sasl),
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 * from hello_table">>),
%% -------------------------------------------------------------------
%% Records Fetch: 2
%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Recs = emysql_util:as_record(
Result, hello_record, record_info(fields, hello_record)),
%% -------------------------------------------------------------------
io:format("~n~s~n", [string:chars($-,72)]),
%% -------------------------------------------------------------------
%% Records Use: 3
%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[begin
io:format("Record: ~p~n", [Rec#hello_record.hello_text])
end || Rec <- Recs],
%% -------------------------------------------------------------------
ok.