# MySQL/OTP User's Guide

MySQL/OTP is a driver for connecting Erlang/OTP applications to MySQL databases. It is a native implementation of the MySQL protocol in Erlang.

This is the documentation generated from the Erlang source code using EDoc. The project page is on Github: https://github.com/mysql-otp/mysql-otp/.

For the reference manual see the mysql module.

## Value representation

MySQL | Erlang | Example
--- | --- | ---
INT, TINYINT, etc. | `integer()` | `42`
VARCHAR, TEXT, etc. | `unicode:chardata()` [1] | `<<"foo">>`, `"bar"`
VARBINARY, BLOB, etc. | `binary()` | `<<1, 2, 3, 4>>`
BIT(N) | `<<_:N/bitstring>>` | `<<255, 6:3>>`
FLOAT, DOUBLE | `float()` | `3.14`
DECIMAL(P, S) | `integer()` when S == 0 `float()` when P =< 15 and S > 0 `binary()` when P >= 16 and S > 0 [2] `{decimal, Value}` [3] (parameter only) | `42` `3.14` `<<"3.14159265358979323846">>` `{decimal, 10.2}`
DATETIME, TIMESTAMP | `calendar:datetime()` [4] | `{{2014, 11, 18}, {10, 22, 36}}`
DATE | `calendar:date()` | `{2014, 11, 18}`
TIME | `{Days, calendar:time()}` [4, 5] | `{0, {10, 22, 36}}`
NULL | `null` | `null`

Notes:

1. When fetching VARCHAR, TEXT etc. they are returned as `binary()`. When sending (insert or update) any `unicode:chardata()` is accepted as input. In a (possibly deep) list of integers and binaries, the integers are treated as Unicode codepoints while binaries are treated as UTF-8 encoded Unicode data. For lists, an error occurs if you try to send invalid Unicode data, but if the input is a pure binary, no validation will be done. This is to allow sending binary non-Unicode data for MySQL's binary strings (BLOB, VARBINARY, etc.).
2. DECIMALs are returned as `integer()` or `float()` when the value can be represented without precision loss and as `binary()` for high precision DECIMAL values. This is similar to how the `odbc` OTP application treats DECIMALs.
3. DECIMALs can be sent as `{decimal, Value}` (where Value is a number, string or binary) but values received from the database are never returned in this format.
4. For `DATETIME`, `TIMESTAMP` and `TIME` values with fractions of seconds, we use a float for the seconds part. (These are unusual and were added to MySQL in version 5.6.4.)
5. Since `TIME` can be outside the `calendar:time()` interval, we use the format as returned by `calendar:seconds_to_daystime/1` for `TIME` values.

## Copying

Copyright 2014-2021 The authors of MySQL/OTP. See the project page at https://github.com/mysql-otp/mysql-otp.

This library is free software licensed under the GNU LGPL which allows you to use it in proprietary applications as well as free software applications with other licenses. This documentation is generated from the source code and thus goes under the same license as the library itself.

Generated by EDoc