forked from jkvor/emysql
-
Notifications
You must be signed in to change notification settings - Fork 208
/
f_load_from_file.erl
100 lines (90 loc) · 3.1 KB
/
f_load_from_file.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
98
99
100
% ------------------------------------------------------------------------
% Emysql: Loading data from a file, a minimal sample
% H. Diedrich <[email protected]> - Eonblast http://www.eonblast.com
% 23 Apr 2013
% Thanks to Qing Liang <https://github.com/qingliangcn>
% ------------------------------------------------------------------------
%
% Re INFILE see MySQL: http://dev.mysql.com/doc/refman/5.1/de/load-data.html
%
% 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> grant file on *.* to hello_username@localhost identified by 'hello_password';
% mysql> quit
%
% If you re-use the database from previous examples, be sure to add the global
% FILE privilege as in the forlast mysql> line above!
%
% On *nix build and run using the batch f_load_from_file in folder samples/:
%
% $ ./f_load_from_file
%
% - or -
%
% Make emysql and start this sample directly, along these lines:
%
% $ cd Emysql
% $ make
% $ cd samples
% $ erlc f_load_from_file.erl
% $ erl -pa ../ebin -s f_load_from_file run -s init stop -noshell
%
% ------------------------------------------------------------------------
%
% Expected Output: (note the result being a list, and the final ok_package)
%
% Connect
% -------
%
% Load Record From File f_hello.txt
% ---------------------------------
%
% Test Select
% -----------
% {result_packet,5,
% [{field,2,<<"def">>,<<"hello_database">>,<<"hello_table">>,
% <<"hello_table">>,<<"hello_text">>,<<"hello_text">>,
% 254,<<>>,33,60,0,0}],
% [[<<"Hello, world!">>]],
% <<>>}
%
% ------------------------------------------------------------------------
-module(f_load_from_file).
-export([run/0]).
run() ->
crypto:start(),
application:start(emysql),
io:format("~nConnect~n-------~n", []),
emysql:add_pool(hello_pool, [{size,1},
{user,"hello_username"},
{password,"hello_password"},
{database,"hello_database"},
{encoding,utf8}]),
io:format("~nClear Table~n-----------~n", []),
emysql:execute(hello_pool,
<<"DELETE FROM hello_table">>),
{ok, Dir} = file:get_cwd(),
io:format("~nLoad Record From File f_hello.txt" ++
"~n---------------------------------~n", []),
Result = emysql:execute(hello_pool,
list_to_binary("LOAD DATA INFILE '" ++ Dir ++
"/f_hello.txt' INTO TABLE hello_table (hello_text)")),
case Result of
{ok_packet,_,_,_,_,_,_} ->
ok;
_ ->
io:format("~n~p~n", [Result]),
io:format("~nNote: you need to grant: " ++
"grant file on *.* to hello_username@localhost identified by" ++
"'hello_password';~n")
end,
io:format("~nTest Select~n-----------~n", []),
Result1 = emysql:execute(hello_pool,
<<"select * from hello_table limit 1">>),
io:format("~p~n", [Result1]),
done.