|
@@ -32,27 +32,28 @@ Opts = [{host, "localhost"}, {user, "foo"}, {password, "hello"},
|
|
%% A query returning results
|
|
%% A query returning results
|
|
{ok, ColumnNames, Rows} = mysql:query(Pid, <<"SELECT * FROM mytable">>),
|
|
{ok, ColumnNames, Rows} = mysql:query(Pid, <<"SELECT * FROM mytable">>),
|
|
|
|
|
|
-%% An "anonymous prepared statement" with parameters, prepared and executed
|
|
|
|
-%% on the fly. NOT IMPLEMENTED YET.
|
|
|
|
-{ok, ColumnNames, Rows} =
|
|
|
|
- mysql:query(Pid, <<"SELECT * FROM mytable WHERE id=?">>, [42]),
|
|
|
|
-
|
|
|
|
%% A query not returning any rows just returns ok.
|
|
%% A query not returning any rows just returns ok.
|
|
-ok = mysql:query(Pid, "INSERT INTO mytable (foo, bar) VALUES (?, ?)",
|
|
|
|
- [1, 42]),
|
|
|
|
|
|
+ok = mysql:query(Pid, "INSERT INTO mytable (foo, bar) VALUES (1, 42)"),
|
|
|
|
+
|
|
|
|
+%% Named prepared statements. Maybe the function should be execute/3
|
|
|
|
+%% instead of query/3.
|
|
|
|
+{ok, foo} = mysql:prepare(Pid, "SELECT * FROM mytable WHERE id=?", foo),
|
|
|
|
+{ok, Columns, Rows} = mysql:query(Pid, foo, [42]),
|
|
|
|
+
|
|
|
|
+%% Unnamed prepared statements. The function query/3 will maybe be
|
|
|
|
+%% renamed to execute/3.
|
|
|
|
+{ok, StmtId} = mysql:prepare(Pid, "SELECT * FROM mytable WHERE id=?"),
|
|
|
|
+{ok, Columns, Rows} = mysql:query(Pid, StmtId, [42]).
|
|
|
|
|
|
%% Separate calls to fetch more info about the last query
|
|
%% Separate calls to fetch more info about the last query
|
|
LastInsertId = mysql:insert_id(Pid),
|
|
LastInsertId = mysql:insert_id(Pid),
|
|
AffectedRows = mysql:affected_rows(Pid),
|
|
AffectedRows = mysql:affected_rows(Pid),
|
|
WarningCount = mysql:warning_count(Pid),
|
|
WarningCount = mysql:warning_count(Pid),
|
|
|
|
|
|
-%% Named prepared statements
|
|
|
|
-{ok, foo} = mysql:prepare(Pid, "SELECT * FROM mytable WHERE id=?", foo),
|
|
|
|
-{ok, Columns, Rows} = mysql:execute(Pid, foo, [42]),
|
|
|
|
-
|
|
|
|
-%% Unnamed prepared statements
|
|
|
|
-{ok, StmtId} = mysql:prepare(Pid, "SELECT * FROM mytable WHERE id=?"),
|
|
|
|
-{ok, Columns, Rows} = mysql:execute(Pid, StmtId, [42]).
|
|
|
|
|
|
+%% An "anonymous prepared statement" with parameters, prepared and executed
|
|
|
|
+%% on the fly. NOT IMPLEMENTED YET.
|
|
|
|
+{ok, ColumnNames, Rows} =
|
|
|
|
+ mysql:query(Pid, <<"SELECT * FROM mytable WHERE id=?">>, [42]),
|
|
|
|
|
|
%% Transactions: If an exception (throw/error/exit) occurs, the transaction
|
|
%% Transactions: If an exception (throw/error/exit) occurs, the transaction
|
|
%% is rollbacked without catching the exception. This means transactions are
|
|
%% is rollbacked without catching the exception. This means transactions are
|
|
@@ -73,6 +74,20 @@ catch
|
|
end.
|
|
end.
|
|
```
|
|
```
|
|
|
|
|
|
|
|
+Value representation
|
|
|
|
+--------------------
|
|
|
|
+
|
|
|
|
+ MySQL | Erlang | Examples
|
|
|
|
+--------------------|-------------------------|-------------------
|
|
|
|
+INT, TINYINT, etc. | integer() | 42
|
|
|
|
+VARCHAR, TEXT, etc. | iodata() | <<"foo">>, "bar"
|
|
|
|
+FLOAT, DOUBLE | float() | 3.14
|
|
|
|
+DECIMAL | binary() | <<"3.140">>
|
|
|
|
+DATETIME, TIMESTAMP | calendar:datetime() | {{2014, 11, 18}, {10, 22, 36}}
|
|
|
|
+DATE | calendar:date() | {2014, 11, 18}
|
|
|
|
+TIME | {time, calendar:time()} | {time, {10, 22, 36}} -- **will probably change**
|
|
|
|
+NULL | null | null
|
|
|
|
+
|
|
Problems with Emysql
|
|
Problems with Emysql
|
|
--------------------
|
|
--------------------
|
|
|
|
|