Reprinted: http://www.iteye.com/topic/910030
Erlang connects to the MySQL database
1. Driver download and Installation
1. Download Erlang-mysql-driver
As follows:
SVN checkout http://erlang-mysql-driver.googlecode.com/svn/trunk/ MySQL
(If you have not installed SVN, the attachment provides the source code for download)
2. compile all source codes under SRC
ERlC *. erl
3. Create Ebin under MySQL and copy all *. Beam under SRC to Ebin.
4. Copy the entire MySQL folder to Erlang/lib.
Ii. Database Operations
1Connect to MySQL:
MySQL: start_link (poolid, "localhost", "root", "password", "databasename ").
2 MYSQL: connect (poolid, host ,? Port, user, password, database, undefined,
Reconnect ).% Establish a connection
3Data Operations
Add:
MySQL: Fetch (poolid, <"insert into…"> ).
Query:
MySQL: Fetch (P1, <"select * from user"> ).
4. What should I do after receiving the data?
Fetch (poolid, "select * from user")-> result
% Result = {data, mysqlres} | {updated, mysqlres} |
% {Error, mysqlres}
NQuery: {Data, mysqlres} = fetch (poolid, <"select * from user"> ).
Obtain field name information:
Fieldinfo = MYSQL: get_result_field_info (mysqlres ).
Obtain the field value:
Allrows = MYSQL: get_result_rows (mysqlres)
NUpdate:
{Updated, UD }= MYSQL: Fetch (poolid, <"Update * from user"> ).
Affectedrow = MYSQL: get_result_affected_rows (UD). % get the number of affected records
NError
{Error, err }= fetch (poolid, <"Update * from user"> ).
Reason = MYSQL: get_result_reason (mysqlres). % error cause
Note: encoding, the eighth parameter of MySQL: start_link, is set to utf8 to support Chinese characters.