Version: 0.3 / edown
Authors: You!.
-------------------------------------------------------------- | THIS TEXT IS USED AS A SAMPLE TO ILLUSTRATE MARKEDOC USAGE. | | If you see this in your browser, you succeeded compiling it | | from markdown into an edoc. As you see it's quite complex | | and there is no 'cheating' involved. | --------------------------------------------------------------
Erlang MySQL driver, based on a rewrite at Electronic Arts™. Supports prepared statements and stored procedures. For samples and docs see below.
While you can use mysql via ODBC, using a driver like Emysql should perform better.
This is a continuation fork of emysql with fixes, updates, more docs and samples. emysql is a clean rewrite of erlang-mysql-driver.
«Which fork should I use?» See history.
«Who used this fork?» Electronic Arts.
«How do I ...?» See samples.
Download: https://github.com/Eonblast/Emysql/archives/master
Repository: https://github.com/Eonblast/Emysql
Docs: http://eonblast.github.com/Emysql/
Open Source Erlang MySQL driver efforts are currently a fractured matter, at least for the higher functionality. There are four main choices:
crypto:start(), application:start(emysql).
% emysql:add_pool(PoolName, PoolSize, Username, Password, Host, Port, Database, Encoding) -> % ok | {error, pool_already_exists} % PoolName = atom() % PoolSize = integer() % Username = string() % Password = string() % Host = string() % Port = integer() % Database = string() % Encoding = atom() emysql:add_pool(mypoolname, 1, "username", "mypassword", "localhost", 3306, "mydatabase", utf8).
-record(ok_packet, {seq_num, affected_rows, insert_id, status, warning_count, msg}). -record(error_packet, {seq_num, code, msg}). -record(result_packet, {seq_num, field_list, rows, extra}).
% emysql:execute(PoolName, Statement) -> result_packet() | ok_packet() | error_packet() % PoolName = atom() % Statement = string() | binary() emysql:execute(mypoolname, <<"SELECT * from mytable">>). #result_packet{field_list=[...], rows=[...]} emysql:execute(mypoolname, <<"UPDATE mytable SET bar = 'baz' WHERE id = 1">>). #ok_packet{affected_rows=1}
% emysql:prepare(StmtName, Statement) -> ok % StmtName = atom() % Statement = binary() | string()
emysql:prepare(my_stmt, <<"SELECT * from mytable WHERE id = ?">>). ok % emysql:execute(PoolName, StmtName, Args) -> result_packet() | ok_packet() | error_packet() % StmtName = atom() % Args = [term()]
emysql:execute(mypoolname, my_stmt, [1]). #result_packet{field_list=[...], rows=[...]}
% emysql:execute(PoolName, StmtName, Args) -> result_packet() | ok_packet() | error_packet() % StmtName = atom() % Args = [term()]
emysql:execute(hello_pool, <<"create procedure sp_hello() begin select * from hello_table; end">>). {ok_packet,1,0,0,2,0,[]}
emysql:execute(hello_pool, <<"call sp_hello();">>). [{result_packet,6, [{field,2,<<"def">>,<<"hello_database">>,<<"hello_table">>, <<"hello_table">>,<<"hello_text">>,<<"hello_text">>, 254,<<>>,33,60,0,0}], [[<<"Hello World!">>],[<<"Hello World!">>]], <<>>}, {ok_packet,7,0,0,34,0,[]}]
% emysql_util:as_record(ResultPacket, RecordName, Fields) -> Result % ResultPacket = result_packet() % RecordName = atom() (the name of the record to generate) % Fields = [atom()] (the field names to generate for each record) % Result = [record()] -module(fetch_example). -record(foo, {bar, baz, bat}). fetch_foo() -> Result = emysql:execute(pool1, <<"select bar, baz, bat from foo">>), Recs = emysql_util:as_record(Result, foo, record_info(fields, foo)), [begin io:format("foo: ~p, ~p, ~p~n", [Foo#foo.bar, Foo#foo.baz, Foo#foo.bat]) end || Foo <- Recs].
$ git clone git://github.com/Eonblast/Emysql.git Emysql
-module(a_hello). -export([run/0]). run() -> crypto:start(), application:start(emysql), emysql:add_pool(hello_pool, 1, "hello_username", "hello_password", "localhost", 3306, "hello_database", utf8), emysql:execute(hello_pool, <<"INSERT INTO hello_table SET hello_text = 'Hello World!'">>), Result = emysql:execute(hello_pool, <<"select hello_text from hello_table">>), io:format("~n~p~n", [Result]).
We come back to that source, but first:
Build emysql.app, using make:
$ cd Emysql $ make
$ mysql [-u<user> -p] mysql> create database hello_database; mysql> use hello_database; mysql> create table hello_table (hello_text char(20)); mysql> grant all privileges on hello_database.* to hello_username@localhost identified by 'hello_password';
Be sure to have ./ebin in your Erlang path. Now copy the Hello World source above at '*' into a file hello.erl and run it (in the Emysql root directory):
$ erlc hello.erl $ erl -pa ../ebin -s hello run -s init stop -noshell
See more sample programms, below.
Sample programs are in ./samples.
To run the samples, create the database as listed above at localhost, and:
$ cd samples $ ./a_hello $ ./b_raw $ ./d_rows_as_records $ ./e_prepared_statement $ ./e_stored_procedure
or make emysql.app and start a_hello etc. manually along these lines (but first create the database as listed above):
$ make $ cd samples $ erlc a_hello.erl $ erl -pa ../ebin -s a_hello run -s init stop -noshell
†maintained at the time of writing, Jan 2011.
Copyright © 2009-2011 Bill Warnecke bill@rupture.com, Jacob Vorreuter jacob.vorreuter@gmail.com, Henning Diedrich hd2010@eonblast.com, Eonblast Corporation http://www.eonblast.com.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Generated by EDoc, Feb 18 2011, 13:04:43.