erlang连接mysql数据库后为什么fetch说匹配不到这个函数
发布网友
发布时间:2022-05-07 05:48
我来回答
共2个回答
热心网友
时间:2022-05-07 07:17
需要注意,官方放在googlecode的测试代码已经旧了,fetch接口实际上需要一个二进制list,所以是[<<"select * from table">>]格式,而不是<<”select * from table”>>格式。
以下是增、删、改、查操作的测试代码:
-mole(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, [<<"SELECT * FROM player">>]),
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, [<<"SELECT * FROM player">>]),
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, [<<"SELECT * FROM player">>]),
io:format("Result4: ~p~n", [Result6]),
ok.