erlang使用emsql连接mysql数据库

最近在找erlang连接mysql案例中发现大部分文章都是复制粘贴或者无法运行没有参考价值,后面自己大概整理下可运行的案例

完整Demo 链接:https://pan.baidu.com/s/1RSX8tmIPPuMCkkRT9oZDUA

提取码:ji7z

首先下载emysql驱动程序,下载地址 https://github.com/Eonblast/Emysql

把include下的文件和src下的文件以及app文件拷贝到对应的项目路径

 

备注:crypto_compat.hrl文件是我编译项目时发现少了,后面去其他项目找了一份

Emakefile文件

{ 
  [ 
    "src/*",
    'src/*/*'

  ], 
  [
    debug_info,
    {i, "include"}, 
    {outdir, "./ebin"} 
  ] 
}.  

Script目录下新建win脚本编译emysql

这是官方给的demo

-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]).

 

返回的Result = ok_packet() | result_packet() | error_packet(),输出一下的话会发现是个复杂的元祖列表结构,需要自己再提取对应内容

所以改了下

%%%-------------------------------------------------------------------
%%% @author admin
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 16. 三月 2020 17:35
%%%-------------------------------------------------------------------
-module(demo).
-author("admin").



%% API
-export([run/0,get_all/0,get_one/0,execute/1,execute/2]).

-include("test.hrl").

-define(DB_POOL, db_pool).

run() ->

    crypto:start(),
    application:start(emysql),

    emysql:add_pool(hello_pool, [{size,1},
        {user,"root"},
        {password,""},
        {database,"test"},
        {encoding,utf8}]),


    io:format("ok ~n").

get_all()->
    Sql = "select * from user",
    %%    # result_packet{field_list=[...], rows=[...]}
    Result = emysql:execute(?DB_POOL,Sql),
    io:format("~n~p~n", [Result]),
    %% Output:
%% ```
%% example: {result_packet,32,
%%               [{field,2,<<"def">>,<<"数据库">>,
%%                        <<"表名">>,<<"表名">>,
%%                        <<"字段1">>,<<"字段1">>,254,<<>>,33,
%%                        60,0,0},
%%                  {field,3,<<"def">>,<<"数据库">>,
%%%%                        <<"表名">>,<<"表名">>,
%%%%                        <<"字段2">>,<<"字段2">>,254,<<>>,33,
%%%%                        60,0,0},
%%%                 .....],
%%                [[<<"Hello World!">>]],
%%                <<>>}

%%    Recs = emysql:as_record(Result, user, record_info(fields, user)),
%%    io:format("~n~p~n", [Recs])
    case Result of
        {result_packet, _, _, R, _} -> R;
        {error_packet, _, _, _, Reason} -> mysql_halt([Sql, Reason]);
        unavailable -> mysql_halt([connect_error, unavailable])
    end.

get_one()->
    Sql = "select * from user where id = ~p",
    Args = [1],
    case emysql:execute(?DB_POOL, io_lib:format(Sql, Args)) of
        {result_packet, _, _, [], _} -> null;
        {result_packet, _, _, [[R|_]|_], _} -> R;
        {error_packet, _, _, _, Reason} -> mysql_halt([io_lib:format(Sql, Args), Reason]);
        unavailable -> mysql_halt([connect_error, unavailable])
    end.

%% 执行一条Sql语句, 返回影响的行数
execute(Sql) ->
    case emysql:execute(?DB_POOL, Sql) of
        {ok_packet, _, Rows, _, _, _, _} -> Rows;
        {result_packet, _, _, R, _} -> R;
        {error_packet, _, _, _, Reason} -> mysql_halt([Sql, Reason]);
        unavailable -> mysql_halt([connect_error, unavailable])
    end.
execute(StmtName, Args) when is_atom(StmtName), is_list(Args) ->
    case emsql:execute(?DB_POOL, StmtName, Args) of
        {ok_packet, _, Rows, _, _, _, _} -> Rows;
        {result_packet, _, _, R, _} -> R;
        {error_packet, _, _, _, Reason} -> mysql_halt([StmtName, Reason]);
        unavailable -> mysql_halt([connect_error, unavailable])
    end.

%%------------------------------------------------------
%% private function
%%------------------------------------------------------
%% @doc 显示人可以看得懂的错误信息
%% 用catch捕捉得到的错误类型是
%% error:{db_error, [Sql, Reason]}
mysql_halt([Sql, Reason]) ->
    erlang:error({db_error, [Sql, Reason]}).

性能方面的话可以参考https://blog.csdn.net/gohuge/article/details/82456946

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章