| void InsertRecord(Table& t) { Insert i(t); i << t["ord_id"] << t["prod_id"] << t["ord_date"] << t["ord_qty"]; // columns i >> 60000 >> 100001 >>"10-MAR-01" >> (float)10; // insert data cout << i.Row() << " rows inserted" << endl; } |
| void UpdateRecord(Table& t) { Update u(t); u << t["ord_qty"]; // update column u >> (float)20; // update data u >> t["ord_id"] == 60000; // where clause cout << u.Row() << " rows updated" << endl; } |
| void DeleteRecord(Table& t) { Delete d(t); d >> t["ord_id"] == 60000; // where clause cout << d.Row() << " rows deleted" << endl; } |
| void SelectRecord(Table& t, Select& s) { s << t["prod_id('0999999')"] << t["ord_id"] << t["ord_date('YYYYMMDD:MI:SS')"] << t["ord_qty('999999.99')"]; // selected columns s >> t["ord_id"] == 60000; // where clause } |
| int main() // example of simple insert, update and delete { Connect c; // use default connection try { Database db = c.OpenDatabase("demo", "demo", "data1"); // database = 'data1' Table tb = db.GetTable("orders"); Select sel(tb); SelectRecord(tb, sel); // define columns and formats for display InsertRecord(tb); while (sel.Row()) cout << sel; // display as line (by default) UpdateRecord(tb); while (sel.Row()) cout << sel; DeleteRecord(tb); while (sel.Row()) cout << sel; // no rows fetched db.Close(); } catch (OException& e) { cerr << e.Show() << endl; } catch (...) { cerr << "Error ..." << endl; } return 0; next page / home page } |
| #include <orac++.h> |
| Example of a C++ program for a simple insert, update and delete records. Please note, that formatting is used in SelectRecord function. All valid Oracle formats can be used. Fetched data can also be loaded into program variables or arrays (using array fetch). Multiple rows can be inserted from arrays of data. In the example below, connection to a local database by specifying Oracle SID ('data1' ) is used. |