一、erlang中使用mysql客户端
使用git获取,https://github.com/dizzyd/erlang-mysql-driver.git
二、common.hrl定义
%%%------------------------------------------------ %%% File : common.hrl %%% Author : yunnet %%% Created : 2013-08-14 %%% Description: 公共定义 %%%------------------------------------------------ %%数据库连接 -define(DB, mysql_conn_poll). -define(DB_HOST, "127.0.0.1"). -define(DB_PORT, 3306). -define(DB_USER, "root"). -define(DB_PASS, "root"). -define(DB_NAME, "track"). -define(DB_ENCODE, utf8). -define(C_TUSER_SQL, "create table tUser (id int not null auto_increment, sUsername varchar(50) not null,sPassword varchar(20) not null, nSex int null, primary key (id)) engine = myisam"). -define(C_TUSER_NAME, "tUser").
三、mysqlLib.erl简单封装
%%%------------------------------------------------ %%% File : mysql_test.erl %%% Author : yunnet %%% Created : 2013-08-14 %%% Description: mysql测试单元 %%%------------------------------------------------ -module(mysql_lib). -compile(export_all). -include("common.hrl"). printf(_Data) -> io:format("this is test data: ~p~n", [_Data]). connect()-> mysql:start_link(?DB, ?DB_HOST, ?DB_PORT, ?DB_USER, ?DB_PASS, ?DB_NAME, fun(_, _, _, _) -> ok end, ?DB_ENCODE), Result = mysql:connect(?DB, ?DB_HOST, ?DB_PORT, ?DB_USER, ?DB_PASS, ?DB_NAME, ?DB_ENCODE, true), case Result of {ok, ConnPid} -> ok; {error, Reason} -> error end, ok. createTable(_Table, _SQLText)-> mysql:fetch(?DB, io_lib:format(<<"drop table if exists '~s'">>, [_Table])), mysql:fetch(?DB, io_lib:format(<<"'~s'">>, [_SQLText])), %%mysql:fetch(?DB, <<"create table tUser (id int not null auto_increment, sUsername varchar(50) not null,sPassword varchar(20) not null, nSex int null, primary key (id)) engine = myisam">>), ok. %%增加用户信息 addUser(_User, _Password, _Sex) -> Sql_Text = io_lib:format(<<"insert into tUser (sUsername, sPassword, nSex) values ('~s','~s',~p)">>, [_User, _Password, _Sex]), A = list_to_binary(Sql_Text), io:format("~p~n", [A]), executeSQL(?DB, Sql_Text), ok. %%增加地理信息 addGpsDesc(_Content) -> io:format("address: ~ts~n", [list_to_binary([_Content])]), Sql_Text = io_lib:format(<<"insert into gps_desc(sXmlContent) values('~ts')">>, [unicode:characters_to_list([_Content])]), io:format("Sql_Text: ~ts~n", [list_to_binary(Sql_Text)]), executeSQL(?DB, Sql_Text), ok. %%写入数据库 %%_SqlText=sql语句, _DB=连接原子名称 executeSQL(_DB, _SqlText)-> mysql:fetch(_DB, _SqlText). %%读数据库 %%_SqlText=sql语句, _DB=连接原子名称 %%返回查询数据列表 executeQuery(_DB, _SqlText)-> case mysql:fetch(_DB, _SqlText) of {data, MySQLRes}-> case mysql:get_result_rows(MySQLRes) of []->[]; List->List end; _->[] end.
四、获取地理信息
-module(takeUrl). -import(lists, [reverse/1]). -import(html_util, [escape/1, unescape/1]). -include_lib("xmerl/include/xmerl.hrl"). -compile(export_all). -record(xmlResponse, { lat, lng, address, business, streetNumber, street, district, city, province, cityCode }). start() -> ok = http_start(), ok = mysql_start(). %% inets start http_start() -> ok = inets:start(), io:format("inets start ok.~n"). %% mysql start mysql_start() -> ok = mysql_lib:connect(), io: format("mysql connect ok.~n"). stop() -> ok = inets:stop(). %%取横坐标 main() -> for(23107237, 23107347, fun(I) -> vertical(I) end), ok. %%取竖坐标 vertical(I) -> for(113269804, 113269914, fun(A) -> get_http(I/1000000, A/1000000) end), ok. %% 根据URL获取内容 get_http(_Lat, _Lng) -> Url = io_lib:format("http://api.map.baidu.com/geocoder?output=xml&location=~f,%20~f&key=37492c0ee6f924cb5e934fa08c6b1676", [_Lat, _Lng]), {ok, Response} = get_Result(Url), {ok, Address} = parseXml(Response), mysql_lib:addGpsDesc(Address). %% 根据获取的内容保存到文件 get_Result(_Url) -> {ok, {{Version, 200, ReasonPhrase}, Headers, Body}} = httpc:request(get, {_Url, []}, [], []), {ok, Body}. %% io:format("Version:~p~n ReasonPhrase:~p~n Headers:~p~n Body: ~p~n", [Version, ReasonPhrase, Headers, Body]), %% @doc Parses Response data. parseXml(_Response) -> Fixed = fix_unicode(_Response), {Doc, _} = xmerl_scan:string(Fixed), [LatNode] = xmerl_xpath:string("//lat", Doc), %% 查找唯一节点 [LatContent] = LatNode#xmlElement.content, #xmlResponse.lat = string:to_float(LatContent#xmlText.value), [LngNode] = xmerl_xpath:string("//Lng", Doc), [LngContent] = LngNode#xmlElement.content, #xmlResponse.lng = string:to_float(LngContent#xmlText.value), [AddressNode] = xmerl_xpath:string("//formatted_address", Doc), [AddressContent] = AddressNode#xmlElement.content, #xmlResponse.address = AddressContent#xmlText.value, [BusinessNode] = xmerl_xpath:string("//business", Doc), [BusinessContent] = BusinessNode#xmlElement.content, #xmlResponse.business = BusinessContent#xmlText.value, [CityCodeNode] = xmerl_xpath:string("//cityCode", Doc), [CityCodeContent] = CityCodeNode#xmlElement.content, #xmlResponse.cityCode = string:to_integer(CityCodeContent#xmlText.value), %%得到节点 %% [Element1] = xmerl_xpath:string("/GeocoderSearchResponse/result/formatted_address", Doc), %% [XmlContent1] = Element1#xmlElement.content, %% Formatted_address = XmlContent1#xmlText.value, %% #xmlResponse.address = Formatted_address, {ok, #xmlResponse.address}. %% @doc Parses an indiviual element into a proplist. This isn't a generic approach. parse_element(Node, Doc) -> [ {Element, Value} || #xmlElement{content = XmlElem} <- xmerl_xpath:string(Node, Doc), #xmlElement{name = Element, attributes = Attributes} <- XmlElem, #xmlAttribute{name = data, value = Value} <- Attributes ]. %% @doc fixes problems with xmerl and unicode fix_unicode(XmlString) -> Binary = unicode:characters_to_binary(XmlString, unicode), binary_to_list(Binary). for(Max, Max, F) -> [F(Max)]; for(I, Max, F) -> [F(I) | for(I + 1, Max, F)].