CompleteDB Embedded: Direct C++ API  2.4.2
CompleteDB Embedded: Direct C++ API Documentation

Quick Start sample

#include "CompleteDb.hpp"
#include <iostream>
using namespace completedb;
int main(int argc, char* argv[]) {
// connect to new or existing database
bool done = db.connect("TickDB");
//------------------------------------------------------------------------
// 1) DDL
//------------------------------------------------------------------------
// CREATE TABLE and columns
CreateTable createTable = db.createTable("Stocks");
createTable.addColumn("Bid", ColumnType::Double);
createTable.addColumn("Ask", ColumnType::Double);
ColumnMetadata colSymbol = createTable.addColumn("Symbol", ColumnType::String);
colSymbol.setLength(8);
done = createTable.execute();
//------------------------------------------------------------------------
// 2) DML
//------------------------------------------------------------------------
// bind columns to the Record statement
Table table = db.getTable("Stocks");
done = table.exists();
Record record = table.getRecord();
ColumnDouble bid = record.getColumnDouble("Bid");
ColumnDouble ask = record.getColumnDouble("Ask");
ColumnString symbol = record.getColumnString("Symbol");
// optional step: prepare bound columns and performs validation
done = record.prepare();
//------------------------------------------------------------------------
// 3) INSERT data using user defined key value: SurrogateId
//------------------------------------------------------------------------
// INSERT (long transaction)
SurrogateId idMSFT = 1;
done = record.insertSurrogate(idMSFT);
symbol.setValue("MSFT");
bid.setValue(31.08);
ask.setValue(31.34);
// INSERT (short transaction)
SurrogateId idGOOG = 2;
done = record.insertSurrogate(idGOOG);
symbol.setValue("GOOG");
bid.setValue(614.29);
ask.setValue(615);
// COMMIT individual row (short transaction)
done = record.commitRow();
// COMMIT long transaction
done = db.commit();
//------------------------------------------------------------------------
// 4) SELECT
//------------------------------------------------------------------------
done = record.loadSurrogate(idMSFT);
std::cout
<< "Symbol: " << symbol.getValue()
<< " Bid: " << bid.getValue()
<< " Ask: " << ask.getValue()
<< std::endl;
//------------------------------------------------------------------------
// 5) DELETE and UPDATE as part of the same transaction
//------------------------------------------------------------------------
// DELETE
done = record.deleteSurrogate(idMSFT);
// UPDATE
done = record.holdSurrogate(idGOOG);
bid.setValue(615);
ask.setValue(615.30);
// COMMIT
done = db.commit();
// error handling example
if (!done) {
std::cout
<< " error: " << db.getLastErrorId()
<< "message: " << db.getLastError()
<< std::endl;
}
//------------------------------------------------------------------------
// 6) TABLE SCAN
//------------------------------------------------------------------------
// print all symbols in the table
record.resetRowIterator();
while (record.nextRow()) {
std::cout
<< symbol.getValue()
<< std::endl;
}
//------------------------------------------------------------------------
// 7) METADATA USAGE
//------------------------------------------------------------------------
// print all tables names
TableIterator itTable = db.getTables();
while (itTable.next()) {
std::cout
<< "Table: " << itTable.getName()
<< std::endl;
// print all columns names
ColumnIterator itColumn = itTable.getColumns();
while (itColumn.next()) {
std::cout
<< "\t"
<< "Column: " << itColumn.getName()
<< std::endl;
}
std::cout << std::endl;
}
return 0;
}