Erlang connects to the MySQL database

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.