MySQL Query Test Code-SQL搜索指令-立哥开发

// Copy Rihgt  2020  Jacky Zong . All rights reserved .
-module(mysql_test).
-compile(export_all).

test() ->
    compile:file("/usr/local/lib/erlang/lib/mysql/mysql.erl"),
    compile:file("/usr/local/lib/erlang/lib/mysql/mysql_conn.erl"),
    
    %% Start the MySQL dispatcher and create the first connection
    %% to the database. 'p1' is the connection pool identifier.
    mysql:start_link(p1, "localhost", "root", "password", "test"),

    %% Add 2 more connections to the connection pool
    mysql:connect(p1, "localhost", undefined, "root", "password", "test",
          true),
    mysql:connect(p1, "localhost", undefined, "root", "password", "test",
          true),
    
    mysql:fetch(p1, <<"DELETE FROM developer">>),

    mysql:fetch(p1, <<"INSERT INTO developer(name, country) VALUES "
             "('Claes (Klacke) Wikstrom', 'Sweden'),"
             "('Ulf Wiger', 'USA')">>),

    %% Execute a query (using a binary)
    Result1 = mysql:fetch(p1, <<"SELECT * FROM developer">>),
    io:format("Result1: ~p~n", [Result1]),
    
    %% Register a prepared statement
    mysql:prepare(update_developer_country,
          <<"UPDATE developer SET country=? where name like ?">>),
    
    %% Execute the prepared statement
    mysql:execute(p1, update_developer_country, [<<"Sweden">>, <<"%Wiger">>]),
    
    Result2 = mysql:fetch(p1, <<"SELECT * FROM developer">>),
    io:format("Result2: ~p~n", [Result2]),
    
    mysql:transaction(
      p1,
      fun() -> mysql:fetch(<<"INSERT INTO developer(name, country) VALUES "
                "('Joe Armstrong', 'USA')">>),
           mysql:fetch(<<"DELETE FROM developer WHERE name like "
                "'Claes%'">>)
      end),

    Result3 = mysql:fetch(p1, <<"SELECT * FROM developer">>),
    io:format("Result3: ~p~n", [Result3]),
    
    mysql:prepare(delete_all, <<"DELETE FROM developer">>),

    {aborted, { {error, foo}, _}} =
    mysql:transaction(
      p1,
      fun() -> mysql:execute(delete_all),
               throw({error, foo})
      end),

    Result4 = mysql:fetch(p1, <<"SELECT * FROM developer">>),
    io:format("Result4: ~p~n", [Result4]),

    mysql:fetch(p1, <<"DELETE FROM numbers">>),
    mysql:prepare(insert_number,
          <<"INSERT INTO numbers(name, _double) VALUES (?, ?)">>),
    D = 1.0e-16,
    mysql:execute(p1, insert_number, [<<"t1">>, D]),
    Result5 = mysql:fetch(p1, <<"SELECT * FROM numbers WHERE name='t1'">>),
    {data, {mysql_result, _, [[<<"t1">>, D]], _, _, _, _, _}} = Result5,
    mysql:fetch(p1, <<"DELETE FROM numbers">>),
                    
    ok.

猜你喜欢

转载自blog.csdn.net/weixin_45806384/article/details/108143815