亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

Erlang 數(shù)據(jù)庫

Erlang能夠連接到傳統(tǒng)數(shù)據(jù)庫,例如SQL Server和Oracle。Erlang有一個inbuilt odbc library可用于處理數(shù)據(jù)庫的工具。

數(shù)據(jù)庫連接

在我們的示例中,我們將使用Microsoft SQL Server。連接到Microsoft SQL Server數(shù)據(jù)庫之前,請確保已檢查以下指針。

  • 您已經(jīng)創(chuàng)建了數(shù)據(jù)庫TESTDB。

  • 您已經(jīng)在TESTDB中創(chuàng)建了一個表EMPLOYEE。

  • 該表包含字段FIRST_NAME,LAST_NAME,AGE,SEX和INCOME。

  • 用戶ID“ testuser”和密碼“ test123”設(shè)置為訪問TESTDB。

  • 確保您已經(jīng)創(chuàng)建了一個名為usersqlserver的ODBC DSN,它創(chuàng)建了到數(shù)據(jù)庫的ODBC連接

建立連接

要建立與數(shù)據(jù)庫的連接,可以使用以下代碼示例。

Example

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

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

上面程序的輸出如下-

Output

<0.33.0>

關(guān)于上述程序,需要注意以下幾點。

  • odbc庫的啟動方法用于指示數(shù)據(jù)庫操作的開始。

  • 連接方法需要DSN,用戶名和密碼才能連接。

創(chuàng)建數(shù)據(jù)庫表

連接到數(shù)據(jù)庫后的下一步是在我們的數(shù)據(jù)庫中創(chuàng)建表。下面的示例演示如何使用Erlang在數(shù)據(jù)庫中創(chuàng)建表。

-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)")

如果現(xiàn)在檢查數(shù)據(jù)庫,您將看到將創(chuàng)建一個名為EMPLOYEE的表。

將記錄插入數(shù)據(jù)庫

要在數(shù)據(jù)庫表中創(chuàng)建記錄時需要它。

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

Example

-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}

從數(shù)據(jù)庫中獲取記錄

Erlang還具有從數(shù)據(jù)庫中獲取記錄的能力。這是通過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") ]).

上面程序的輸出如下:

Output

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

因此,您可以看到上一節(jié)中的insert命令起作用,并且select命令返回了正確的數(shù)據(jù)。

根據(jù)參數(shù)從數(shù)據(jù)庫中獲取記錄

Erlang還具有根據(jù)某些過濾條件從數(shù)據(jù)庫中獲取記錄的功能。

一個實例如下

-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}]}

從數(shù)據(jù)庫更新記錄

Erlang還具有從數(shù)據(jù)庫更新記錄的功能。

相同的示例如下:

-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}

從數(shù)據(jù)庫中刪除記錄

Erlang還具有從數(shù)據(jù)庫中刪除記錄的功能。

相同的示例如下

-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}

表結(jié)構(gòu)

Erlang還具有描述表結(jié)構(gòu)的功能。

一個實例如下

-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}]}

記錄數(shù)

Erlang還具有獲取表中記錄總數(shù)的功能。

在下面的程序中顯示了相同的示例。

-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}