//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
string filename="departments.sqlite";
//--- criamos ou abrimos um banco de dados na pasta compartilhada do terminal
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;
}
//--- criamos a tabela COMPANY
if(!CreataTableCompany(db))
{
DatabaseClose(db);
return;
}
//--- criamos a tabela DEPARTMENT
if(!CreataTableDepartment(db))
{
DatabaseClose(db);
return;
}
//--- exibimos a lista de todos os campos na tabela COMPANY e DEPARTMENT
PrintFormat("Try to print request \"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()\") failed, error code=%d", GetLastError());
DatabaseClose(db);
return;
}
//--- exibimos no log a tabela COMPANY
PrintFormat("Try to print request \"SELECT * from COMPANY\"");
if(DatabasePrint(db, "SELECT * from COMPANY", 0)<0)
{
Print("DatabasePrint failed with code ", GetLastError());
DatabaseClose(db);
return;
}
//--- texto da consulta para junção das tabelas COMPANY e DEPARTMENT
string request="SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT "
"ON COMPANY.ID = DEPARTMENT.EMP_ID";
//--- exibimos o resultado da junção de tabelas
PrintFormat("Try to print request \"SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT\"");
if(DatabasePrint(db, request, 0)<0)
{
Print("DatabasePrint failed with code ", GetLastError());
DatabaseClose(db);
return;
}
//--- fechamos o banco de dados
DatabaseClose(db);
}
/*
Conclusão:
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
*/
//+------------------------------------------------------------------+
//| Cria a tabela COMPANY |
//+------------------------------------------------------------------+
bool CreataTableCompany(int database)
{
//--- se a tabela COMPANY existir, vamos exclui-la
if(DatabaseTableExists(database, "COMPANY"))
{
//--- excluímos a tabela
if(!DatabaseExecute(database, "DROP TABLE COMPANY"))
{
Print("Failed to drop table COMPANY with code ", GetLastError());
return(false);
}
}
//--- criamos a tabela 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: create table COMPANY failed with code ", GetLastError());
return(false);
}
//--- inserimos os dados na tabela 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("COMPANY insert failed with code ", GetLastError());
return(false);
}
//--- sucesso
return(true);
}
//+------------------------------------------------------------------+
//| Cria a tabela DEPARTMENT |
//+------------------------------------------------------------------+
bool CreataTableDepartment(int database)
{
//--- se a tabela DEPARTMENT existir, vamos exclui-la
if(DatabaseTableExists(database, "DEPARTMENT"))
{
//--- excluímos a tabela
if(!DatabaseExecute(database, "DROP TABLE DEPARTMENT"))
{
Print("Failed to drop table DEPARTMENT with code ", GetLastError());
return(false);
}
}
//--- criamos a tabela 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: create table DEPARTMENT failed with code ", GetLastError());
return(false);
}
//--- inserimos os dados na tabela 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("DEPARTMENT insert failed with code ", GetLastError());
return(false);
}
//--- sucesso
return(true);
}
//+-------------------------------------------------------------------
|