下载源码:
svn checkout http://erlang-mysql-driver.googlecode.com/svn/trunk/ erlang-mysql-driver-read-only
下载完后命名目录为"mysql",进入mysql/src目录对其下的文件全部进行编译;
建立mysql/ebin目录,把编译好的.beam文件拷贝到ebin中
复制mysql目录到erlang/lib目录下。
启动erlang sehll环境
#erl
连接mysql:
1>mysql:start_link(p1, "localhost", "root", "password", "test").
mysql_conn:620: greeting version "5.0.45" (protocol 10) salt ">H1XKN%k" caps 41516 serverchar <<8,2,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0>>salt2 "mc)>?aI*6pR("
mysql_auth:187: mysql_auth send packet 1: <<5,162,0,0,64,66,15,0,8,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
114,111,111,116,0,20,68,156,106,10,
55,204,242,199,179,117,105,185,82,
223,141,254,117,211,113,246>>
mysql_auth:187: mysql_auth send packet 3: <<75,77,82,65,65,82,73,72,0>>
mysql_conn:426: fetch <<"use test">> (id <0.49.0>)
{ok,<0.47.0>}
更新数据操作
2>mysql:fetch(p1,<<"insert into t(id,name)values('12','laja')">>).
mysql_conn:426: fetch <<"insert into t(id,name)values('12','laja')">> (id <0.49.0>)
{updated,{mysql_result,[],[],1,[]}} #1表示影响数
查询数据操作
3> mysql:fetch(p1,<<"select * from t">>).
mysql_conn:426: fetch <<"select * from t">> (id <0.49.0>)
{data,{mysql_result,[{<<"t">>,<<"id">>,11,'LONG'},
{<<"t">>,<<"name">>,20,'STRING'}],
[12,<<"laja">>]],
0,[]}}
数据格式说明:
{data, MySQLRes} | {updated, MySQLRes} | {error, MySQLRes} with: MySQLRes = term()
FieldInfo = mysql:get_result_field_info(MysqlRes) AllRows = mysql:get_result_rows(MysqlRes) with: FieldInfo = list() of {Table, Field, Length, Name} AllRows = list() of list() representing records
Affected = mysql:get_result_affected_rows(MysqlRes) with: Affected = integer()
Reason = mysql:get_result_reason(MysqlRes) with: Reason = string()
至此,erlang连接mysql已经ok了。