English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Database Erlang

Erlang può connettersi a database tradizionali come SQL Server e Oracle. Erlang ha unaLibreria ODBC integrataStrumenti disponibili per la gestione del database.

Connessione al database

Nel nostro esempio, utilizzeremo Microsoft SQL Server. Assicurati di aver verificato i seguenti punti prima di connetterti al database Microsoft SQL Server.

  • Hai creato il database TESTDB.

  • Hai creato una tabella EMPLOYEE nel TESTDB.

  • La tabella contiene i campi FIRST_NAME, LAST_NAME, AGE, SEX e INCOME.

  • L'ID utente "testuser" e la password "test123" sono impostati per accedere a TESTDB.

  • Assicurati di aver creato un DSN ODBC chiamato usersqlserver che crea una connessione ODBC al database.

Stabilisci la connessione

Per stabilire una connessione con il database, puoi utilizzare l'esempio di codice seguente.

Esempio

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

L'output del programma sopra è il seguente-

io:fwrite("~p",[odbc:sql_query(Ref, "SELECT * FROM EMPLOYEE") ]).

<0.33.0>

Per quanto riguarda il programma sopra, è necessario notare i seguenti punti.

  • Il metodo di avvio della libreria odbc viene utilizzato per indicare l'inizio delle operazioni di database.

  • Il metodo di connessione richiede DSN, nome utente e password per connettersi.

Creazione di una tabella del database

Il passo successivo dopo la connessione al database è creare una tabella nel nostro database. Il seguente esempio dimostra come creare una tabella nel database utilizzando Erlang.

-module(helloworld). 
-export([start/0]). 
start() -> 
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver; UID = testuser; PWD = test123", []), 
   odbc:sql_query(Ref, "CREA TABELLA EMPLOYEE (FIRSTNAME char varying(20), 
   LASTNAME char varying(20), AGE integer, SEX char(1), INCOME integer")

Se controlli ora il database, vedrai che viene creato un record chiamatoEMPLOYEEdella tabella.

Inserisci il record nel database

è necessario per creare un record in una tabella del database.

Il seguente esempio inserisce un record nella tabella employee. Se la tabella viene aggiornata con successo, viene restituito il valore del record aggiornato e il numero di record aggiornati.

Esempio

-module(helloworld). 
-export([start/0]). 
start() -> 
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver; UID = testuser;PWD = test123", []), 
   io:scrivi("~p",[odbc:sql_query(Ref, 
   "INSERISCI IN EMPLOYEE VALORI('Mac', 'Mohan', 20, 'M', 2000)")]).

L'output del programma sopra sarà

{updated,1}

从数据库中获取记录

Ottieni record dal database

Erlang ha anche la capacità di ottenere record dal database. Questo è fatto tramite il metodo sql_query.

-module(helloworld). 
-export([start/0]). 
start() ->
   odbc:start(), 
   {ok, Ref} = odbc:connect("DSN = usersqlserver; UID = testuser;PWD = test123", []), 
   Ecco un esempio nel programma sottostante:

L'output del programma sopra sarà come segue:

io:fwrite("~p",[odbc:sql_query(Ref, "SELECT * FROM EMPLOYEE") ]).

[{{sql_char, 1}, ["M"]}])]).
{selected,["FIRSTNAME","LASTNAME","AGE","SEX","INCOME"],

Output

Quindi, come potete vedere, il comando insert nella sezione precedente è stato eseguito correttamente e il comando select ha restituito i dati corretti.

Ottieni record dal database tramite parametri

Un esempio è il seguente:

-module(helloworld). 
-export([start/0]). 
start() -> 
   odbc:start(), 
   Erlang ha anche la funzione di ottenere record dal database in base a determinati criteri di filtraggio. 
   {ok, Ref} = odbc:connect("DSN=usersqlserver; UID=testuser;PWD=test123", []), 
   io:fwrite("~p",[ odbc:param_query(Ref, "SELECT * FROM EMPLOYEE WHERE SEX=?",

L'output del programma sopra sarà:

[{{sql_char, 1}, ["M"]}])]).
         {selected,["FIRSTNAME","LASTNAME","AGE","SEX","INCOME"],

[{"Mac","Mohan",20,"M",2000}]}

Erlang ha anche la funzione di aggiornare record nel database.

Ecco un esempio simile:

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

L'output del programma sopra sarà:

{updated,1}

Eliminazione record dal database

Erlang ha anche la funzione di eliminare record dal database.

Ecco un esempio simile:

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

L'output del programma sopra sarà come segue:

{updated,1}

Struttura della tabella

Erlang ha anche la funzione di descrivere la struttura delle tabelle.

Un esempio è il seguente

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

L'output del programma sopra sarà come segue:

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

Numero di record

Erlang ha anche la funzione di ottenere il numero totale di record nella tabella.

Nella seguente programma è mostrato lo stesso esempio.

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

L'output del programma sopra sarà

{ok,1}