展开全部
需要注意,官方放在googlecode的测试代码已经旧e69da5e887aa62616964757a686964616f31333337383865了,fetch接口实际上需要一个二进制list,所以是[<>]格式,而不是<>格式。
以下是增、删、改、查操作的测试代码:-module(mysql_test).
-export([start/0]).
start () ->
% 连接数据库
mysql:start_link(conn1, "localhost", "root", "ybybyb", "webgame"),
% 插入数据
Result1 = mysql:fetch(
conn1,
[<<
"INSERT INTO"
" `player`"
" SET"
" `username` = 'test',"
" `joined_datetime` = now(),"
" `logined_datetime` = now();"
>>]
),
io:format("Result1: ~p~n", [Result1]),
% 查询数据
Result2 = mysql:fetch(conn1, [<>]),
io:format("Result2: ~p~n", [Result2]),
% 更新数据
Result3 = mysql:fetch(
conn1,
[<<
"UPDATE"
" `player`"
"SET"
" `username` = 'test_player'"
"where"
" `username` = 'test'"
>>]
),
io:format("Result3: ~p~n", [Result3]),
% 查询数据
Result4 = mysql:fetch(conn1, [<>]),
io:format("Result4: ~p~n", [Result4]),
% 删除数据
Result5 = mysql:fetch(
conn1,
[<<
"DELETE FROM `player` WHERE `username` = 'test_player'"
>>]
),
io:format("Result5: ~p~n", [Result5]),
% 查询数据
Result6 = mysql:fetch(conn1, [<>]),
io:format("Result4: ~p~n", [Result6]),
ok.