//+------------------------------------------------------------------+ //| Script programı başlatma fonksiyonu | //+------------------------------------------------------------------+ void OnStart() { string filename="departments.sqlite"; //--- ortak terminal klasöründe veritabanı oluşturma veya açma 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; } //--- COMPANY tablosu oluştur if(!CreataTableCompany(db)) { DatabaseClose(db); return; } //--- DEPARTMENT tablosu oluştur if(!CreataTableDepartment(db)) { DatabaseClose(db); return; } //--- COMPANY ve DEPARTMENT tablolarındaki tüm alanların listesini görüntüle 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; } //--- günlükte COMPANY tablosunu görüntüle 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; } //--- COMPANY ve DEPARTMENT tablolarını birleştirmek için istek metni string request="SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT " "ON COMPANY.ID = DEPARTMENT.EMP_ID"; //--- tabloları birleştirmenin sonucunu göster 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; } //--- veritabanını kapat DatabaseClose(db); } /* Sonuç: 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 */ //+------------------------------------------------------------------+ //| COMPANY tablosu oluştur | //+------------------------------------------------------------------+ bool CreateTableCompany(int database) { //--- COMPANY tablosu varsa, sil if(DatabaseTableExists(database, "COMPANY")) { //--- tabloyu sil if(!DatabaseExecute(database, "DROP TABLE COMPANY")) { Print("Failed to drop table COMPANY with code ", GetLastError()); return(false); } } //--- COMPANY tablosu oluştur 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); } //--- COMPANY tablosuna veri gir 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); } //--- başarı return(true); } //+------------------------------------------------------------------+ //| DEPARTMENT tablosu oluştur | //+------------------------------------------------------------------+ bool CreateTableDepartment(int database) { //--- DEPARTMENT tablosu varsa, sil if(DatabaseTableExists(database, "DEPARTMENT")) { //--- tabloyu sil if(!DatabaseExecute(database, "DROP TABLE DEPARTMENT")) { Print("Failed to drop table DEPARTMENT with code ", GetLastError()); return(false); } } //--- DEPARTMENT tablosu oluştur 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); } //--- DEPARTMENT tablosuna veri gir 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); } //--- başarı return(true); } //+------------------------------------------------------------------- |