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
| -module(sqlite).
| -compile(export_all).
|
| query(Sql, DatabaseName) ->
| {ok, DB} = esqlite3:open(DatabaseName),
| Maps = queryFromDb(DB, Sql),
| esqlite3:close(DB),
| Maps.
|
| execute(Sql1, DatabaseName, SqlFileName) ->
| MyList = case unicode:characters_to_list(Sql1) of
| L when is_list(L) -> L;
| _ -> binary_to_list(Sql1)
| end,
| {ok,G} = file:open(SqlFileName,[write,{encoding,utf8}]),
| io:put_chars(G,MyList),
| file:close(G),
|
| {ok,Sql} = file:read_file(SqlFileName),
| file:delete(SqlFileName),
|
| {ok, DB} = esqlite3:open(DatabaseName),
| ok = esqlite3:exec(Sql, DB),
| esqlite3:close(DB).
|
| bitStrToStr([]) -> [];
| bitStrToStr([Value | T]) ->
| if
| is_bitstring(Value) == true ->
| TA = binary:bin_to_list(Value);
| is_atom(Value) == true ->
| TA = atomToStr(Value);
| is_integer(Value) == true ->
| TA = lists:flatten(io_lib:format("~w", [Value]));
| true ->
| TA = lists:flatten(io_lib:format("~w", [Value]))
| end,
| [TA | bitStrToStr(T)].
|
|
| atomToStr(Value) ->
| ResTmp = lists:flatten(io_lib:format("~w", [Value])),
| Pos = string:str(ResTmp, "'"),
| if
| Pos == 1 ->
| Res = string:strip(ResTmp, both, $');
| true ->
| Res = ResTmp
| end,
| Res.
|
| atomListToStrList([]) -> [];
| atomListToStrList([Value | T]) ->
| [atomToStr(Value) | atomListToStrList(T)].
|
|
| rowsToList(Names, Row) ->
| A = atomListToStrList(tuple_to_list(Names)),
| B = bitStrToStr(tuple_to_list(Row)),
| lists:zip(A, B).
|
| queryFromDb(DB, Sql) ->
| AFun = fun(Names, Row) ->
| rowsToList(Names, Row)
| end,
| Maps = esqlite3:map(AFun, Sql, DB),
| Maps.
|
|