//+------------------------------------------------------------------+
//| Funzione di start del programma script |
//+------------------------------------------------------------------+
void OnStart()
{
string filename="departments.sqlite";
//--- crea o apre il database nella cartella terminale comune
int db=DatabaseOpen(filename, DATABASE_OPEN_READWRITE | DATABASE_OPEN_CREATE |DATABASE_OPEN_COMMON);
if(db==INVALID_HANDLE)
{
Print("DB: ", filename, " open failed with code ", GetLastError());
return;
}
//--- crea la tabella COMPANY
if(!CreataTableCompany(db))
{
DatabaseClose(db);
return;
}
//--- crea la tabella DEPARTMENT
if(!CreataTableDepartment(db))
{
DatabaseClose(db);
return;
}
//--- mostra l'elenco di tutti i campi nelle tabelle COMPANY e DEPARTMENT
PrintFormat("Prova a stampare la richiesta \"PRAGMA TABLE_INFO(COMPANY);PRAGMA TABLE_INFO(DEPARTMENT)\"");
if(DatabasePrint(db, "PRAGMA TABLE_INFO(COMPANY);PRAGMA TABLE_INFO(DEPARTMENT)", 0)<0)
{
PrintFormat("DatabasePrint(\"PRAGMA TABLE_INFO()\") fallito, codice errore=%d", GetLastError());
DatabaseClose(db);
return;
}
//--- mostra la tabella COMPANY nel registro
PrintFormat("Prova a stampare la richiesta \"SELECT * da COMPANY\"");
if(DatabasePrint(db, "SELECT * from COMPANY", 0)<0)
{
Print("DatabasePrint fallito con codice", GetLastError());
DatabaseClose(db);
return;
}
//--- richiesta di testo per combinare le tabelle COMPANY e DEPARTMENT
string request="SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT "
"ON COMPANY.ID = DEPARTMENT.EMP_ID";
//--- mostra la tabella che combina il risultato
PrintFormat("Prova a stampare la richiesta\"SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT\"");
if(DatabasePrint(db, request, 0)<0)
{
Print("DatabasePrint fallito con codice", GetLastError());
DatabaseClose(db);
return;
}
//--- chiude il database
DatabaseClose(db);
}
/*
Conclusione:
Try to print request "PRAGMA TABLE_INFO(COMPANY);PRAGMA TABLE_INFO(DEPARTMENT)"
#| cid name type notnull dflt_value pk
-+-------------------------------------------
1| 0 ID INT 1 1
2| 1 NAME TEXT 1 0
3| 2 AGE INT 1 0
4| 3 ADDRESS CHAR(50) 0 0
5| 4 SALARY REAL 0 0
#| cid name type notnull dflt_value pk
-+------------------------------------------
1| 0 ID INT 1 1
2| 1 DEPT CHAR(50) 1 0
3| 2 EMP_ID INT 1 0
Try to print request "SELECT * from COMPANY"
#| ID NAME AGE ADDRESS SALARY
-+--------------------------------
1| 1 Paul 32 California 25000.0
2| 2 Allen 25 Texas 15000.0
3| 3 Teddy 23 Norway 20000.0
4| 4 Mark 25 Rich-Mond 65000.0
5| 5 David 27 Texas 85000.0
6| 6 Kim 22 South-Hall 45000.0
7| 7 James 24 Houston 10000.0
Try to print request "SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT"
#| EMP_ID NAME DEPT
-+-------------------------
1| 1 Paul IT Billing
2| 2 Allen Engineering
3| Teddy
4| Mark
5| David
6| Kim
7| 7 James Finance
*/
//+------------------------------------------------------------------+
//| Crea la tabella COMPANY |
//+------------------------------------------------------------------+
bool CreateTableCompany(int database)
{
//--- se esiste la tabella COMPANY, eliminala
if(DatabaseTableExists(database, "COMPANY"))
{
//--- elimina la tabella
if(!DatabaseExecute(database, "DROP TABLE COMPANY"))
{
Print("Fallimento nell'eliminare la tabella COMPANY con codice", GetLastError());
return(false);
}
}
//--- crea la tabella COMPANY
if(!DatabaseExecute(database, "CREATE TABLE COMPANY("
"ID INT PRIMARY KEY NOT NULL,"
"NAME TEXT NOT NULL,"
"AGE INT NOT NULL,"
"ADDRESS CHAR(50),"
"SALARY REAL );"))
{
Print("DB: creazione tabella COMPANY fallita con codice ", GetLastError());
return(false);
}
//--- inserisci i dati nella tabella COMPANY
if(!DatabaseExecute(database, "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Paul', 32, 'California', 25000.00); "
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Texas', 15000.00); "
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Teddy', 23, 'Norway', 20000.00); "
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Mark', 25, 'Rich-Mond', 65000.00); "
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (5, 'David', 27, 'Texas', 85000.0); "
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (6, 'Kim', 22, 'South-Hall', 45000.0); "
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (7, 'James', 24, 'Houston', 10000.00); "))
{
Print("inserimento COMPANY fallito con codice", GetLastError());
return(false);
}
//--- successo
return(true);
}
//+------------------------------------------------------------------+
//| Creare la tabella DEPARTMENT |
//+------------------------------------------------------------------+
bool CreateTableDepartment(int database)
{
//--- se esiste la tabella DEPARTMENT, eliminala
if(DatabaseTableExists(database, "DEPARTMENT"))
{
//--- elimina la tabella
if(!DatabaseExecute(database, "DROP TABLE DEPARTMENT"))
{
Print("Fallimento nell' eliminare la tabella DEPARTMENT con codice", GetLastError());
return(false);
}
}
//--- crea la tabella DEPARTMENT
if(!DatabaseExecute(database, "CREATE TABLE DEPARTMENT ("
"ID INT PRIMARY KEY NOT NULL,"
"DEPT CHAR(50) NOT NULL,"
"EMP_ID INT NOT NULL);"))
{
Print("DB: creazione tabella DEPARTMENT fallita con codice ", GetLastError());
return(false);
}
//--- inserisci i dati nella tabella DEPARTMENT
if(!DatabaseExecute(database, "INSERT INTO DEPARTMENT (ID,DEPT,EMP_ID) VALUES (1, 'IT Billing', 1); "
"INSERT INTO DEPARTMENT (ID,DEPT,EMP_ID) VALUES (2, 'Engineering', 2); "
"INSERT INTO DEPARTMENT (ID,DEPT,EMP_ID) VALUES (3, 'Finance', 7);"))
{
Print("inserimento DEPARTMENT fallito con codice ", GetLastError());
return(false);
}
//--- successo
return(true);
}
//+-------------------------------------------------------------------
|