#include <db/a_dbc.h> //A_DBC h #include <mad/prop.h> //Properties class h //#define TODO exec #define TODO select //Enter data record into Properties, //Needs by demo... void dbcGet(ADBC *dbc, Properties *p){ for (unsigned int i = 0; i < dbc->getFieldCount(); i++){ p->put(dbc->getFieldName(i), dbc->getFieldData(i)); } } //Database select and parse results. void select(ADBC *db, Properties *q){ printf(">OPEN...\n"); //Open dataset if (!db->open()){ printf("Error\n"); exit(1); } //Print general data printf("Database matrix (cols) x (rows): (%d) x (%ld)\n", db->getFieldCount(), db->getRecordCount() ); //Show all records... while (!db->eof()){ dbcGet(db, q); q->dump(); db->next(); } //Close dataset db->close(); } //Exec SQL, no data. void exec(ADBC *db){ printf(">Exec SQL...\n"); //Exec SQL if (!db->exec()){ printf("Error\n"); exit(1); } //Print general data printf("Affect %ld row(s)\n", db->getAffectedCount()); } //Main program int main(void){ //Init MFC, if we using Microsoft Fondation Classes (MFC/ODBC driver) #ifdef __MFC__ if (!initMFC()){ printf("content-type: text/plain\n\nMFC Crashed\nOh, dear!\n"); exit(100); } #endif //Print this for web environment printf("content-type: text/plain\n\n"); //for web testing //Properties for connection to the database Properties *p = new Properties(); //Properties for demo only Properties *q = new Properties(); //Seting Database properties, they are depend of database //These are for Interbase p->put("DATABASE", "/usr/interbase/examples/v4/employee.gdb" ); p->put("DATABASEUSER", "SYSDBA"); p->put("DATABASEPASS", "masterkey"); //OK, everyone know it:) printf(">CREATE...\n"); //Database connection object ADBConnection *dbcon = getADBConnection(); printf(">CONNECT...\n"); //Trying to connect, checking result... if (!dbcon->connect(p)){ printf("Error\n"); exit(1); } //ADBC object for SQL ADBC *db = dbcon->getADBC(); printf(">SQL...\n"); //Put Some SQL here...defined at the top #if (TODO != exec) db->setSQL("select * from ta"); #else db->setSQL("Insert into ta values(123, 'nmmm', 'Bulgaria')"); #endif //Do operation...defined at the top TODO(db, q); //Destroy ADBC object delete(db); printf(">Disconnect...\n"); //Disconnect dbcon->disconnect(); //Clean up and destroy delete(dbcon); delete(p); delete(q); printf(">Exit\n"); //Everything is OK, so return 0 return 0; } |