Erlang - 資料庫



Erlang能夠連線到傳統的資料庫,例如SQL Server和Oracle。Erlang擁有一個內建的odbc庫,可用於處理資料庫。

資料庫連線

在本例中,我們將使用Microsoft SQL Server。在連線到Microsoft SQL Server資料庫之前,請確保檢查以下幾點。

  • 您已建立資料庫TESTDB。

  • 您已在TESTDB中建立表EMPLOYEE。

  • 此表包含欄位FIRST_NAME、LAST_NAME、AGE、SEX和INCOME。

  • 已設定使用者ID“testuser”和密碼“test123”來訪問TESTDB。

  • 確保您已建立名為usersqlserver的ODBC DSN,該DSN建立到資料庫的ODBC連線。

建立連線

要建立到資料庫的連線,可以使用以下程式碼示例。

示例

-module(helloworld). 
-export([start/0]). 

start() ->
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver;UID = testuser;PWD = test123", []), 
   io:fwrite("~p",[Ref]).

上述程式的輸出如下:

輸出

<0.33.0>

關於上述程式,需要注意以下幾點。

  • odbc庫的start方法用於指示資料庫操作的開始。

  • connect方法需要DSN、使用者名稱和密碼才能連線。

建立資料庫表

連線到資料庫後的下一步是在資料庫中建立表。以下示例顯示如何使用Erlang在資料庫中建立表。

示例

-module(helloworld). 
-export([start/0]). 

start() -> 
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver; UID = testuser;PWD = test123, []), 
   odbc:sql_query(Ref, "CREATE TABLE EMPLOYEE (FIRSTNAME char varying(20), 
   LASTNAME char varying(20), AGE integer, SEX char(1), INCOME integer)")

如果您現在檢查資料庫,您將看到已建立名為EMPLOYEE的表。

將記錄插入資料庫

當您想將記錄建立到資料庫表中時,這是必需的。

以下示例將在employee表中插入一條記錄。如果表成功更新,則記錄和語句將返回已更新記錄的值以及已更新的記錄數。

示例

-module(helloworld). 
-export([start/0]). 

start() -> 
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver; UID = testuser;PWD = test123", []), 
   io:fwrite("~p",[odbc:sql_query(Ref, 
   "INSERT INTO EMPLOYEE VALUES('Mac', 'Mohan', 20, 'M', 2000)")]).

上述程式的輸出將為:

輸出

{updated,1}

從資料庫中提取記錄

Erlang還能夠從資料庫中提取記錄。這是透過sql_query方法完成的。

以下程式顯示了一個示例:

示例

-module(helloworld). 
-export([start/0]). 

start() ->
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver; UID = testuser;PWD = test123", []), 
   io:fwrite("~p",[odbc:sql_query(Ref, "SELECT * FROM EMPLOYEE") ]).

上述程式的輸出如下:

輸出

{selected,["FIRSTNAME","LASTNAME","AGE","SEX","INCOME"],
[{"Mac","Mohan",20,"M",2000}]}

因此您可以看到上一節中的insert命令有效,並且select命令返回了正確的資料。

基於引數從資料庫中提取記錄

Erlang還能夠根據某些篩選條件從資料庫中提取記錄。

示例如下:

示例

-module(helloworld). 
-export([start/0]). 

start() -> 
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN=usersqlserver; UID=testuser;PWD=test123", []), 
   io:fwrite("~p",[ odbc:param_query(Ref, "SELECT * FROM EMPLOYEE WHERE SEX=?", 
   [{{sql_char, 1}, ["M"]}])]).

上述程式的輸出將為:

輸出

{selected,["FIRSTNAME","LASTNAME","AGE","SEX","INCOME"],
         [{"Mac","Mohan",20,"M",2000}]}

更新資料庫中的記錄

Erlang還能夠更新資料庫中的記錄。

以下是一個示例:

示例

-module(helloworld). 
-export([start/0]). 

start() -> 
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver; UID = testuser;PWD = test123", []), 
   
   io:fwrite("~p",[ odbc:sql_query(Ref, "
      UPDATE EMPLOYEE SET AGE = 5 WHERE INCOME= 2000")]).

上述程式的輸出將為:

輸出

{updated,1}

從資料庫中刪除記錄

Erlang還能夠從資料庫中刪除記錄。

以下是一個示例:

示例

-module(helloworld). 
-export([start/0]). 

start() -> 
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver; UID = testuser;PWD = test123", []), 
   io:fwrite("~p",[ odbc:sql_query(Ref, "DELETE EMPLOYEE WHERE INCOME= 2000")]).

上述程式的輸出如下:

輸出

{updated,1}

表結構

Erlang還能夠描述表結構。

示例如下:

示例

-module(helloworld). 
-export([start/0]). 

start() -> 
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver; UID = testuser;PWD = test123", []), 
   io:fwrite("~p",[odbc:describe_table(Ref, "EMPLOYEE")]).

上述程式的輸出如下:

輸出

{ok,[{"FIRSTNAME",{sql_varchar,20}},
   {"LASTNAME",{sql_varchar,20}},
   {"AGE",sql_integer},
   {"SEX",{sql_char,1}},
   {"INCOME",sql_integer}]}

記錄計數

Erlang還能夠獲取表中記錄的總數。

以下程式顯示了一個示例。

示例

-module(helloworld). 
-export([start/0]). 

start() ->
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver; UID = sa;PWD = demo123", []), 
   io:fwrite("~p",[odbc:select_count(Ref, "SELECT * FROM EMPLOYEE")]).

上述程式的輸出將為:

{ok,1}
廣告
© . All rights reserved.