🚩Quick start

Here’s how to get started with Q.I.C in a matter of minutes!

Creating a Database

You can create a db with the following code, remember to create your db file before running the code or it wont work.

#include "qic.h"
#include <iostream>
#include <filesystem>

int main() {
    // Create a database instance
    data_base db;

    // Open an existing database file
    std::filesystem::path dbPath = "mydatabase.db";
    db.open_database(dbPath);
    
    std::cout << "Database opened successfully!" << std::endl;

    // Close the database
    db.close();
    std::cout << "Database closed and saved!" << std::endl;

    return 0;
}

Creating a table

#include "qic.h"
#include <iostream>
#include <unordered_map>

int main() {
    data_base db;
    db.open_database("mydatabase.db");

    // Define table schema
    std::unordered_map<std::string, data_type> tableStructure = {
        {"id", INT},
        {"name", STRING},
        {"active", BOOL},
        {"balance", DOUBLE}
    };

    // Add a table to the database
    operation op = db.add_table("users", tableStructure);

    if (op.stat == SUCCESS) {
        std::cout << "Table 'users' created successfully!" << std::endl;
    } else {
        std::cout << "Error creating table: " << op.error << std::endl;
    }

    db.close();
    return 0;
}

Adding Data to the Table

#include "qic.h"
#include <iostream>
#include <unordered_map>
#include <any>

int main() {
    data_base db;
    db.open_database("mydatabase.db");

    // Create a new record
    std::unordered_map<std::string, std::any> newRecord = {
        {"id", std::any(1)},
        {"name", std::any(std::string("Alice"))},
        {"active", std::any(true)},
        {"balance", std::any(2500.35)}
    };

    // Add the record to the 'users' table
    operation op = db.add_value("users", newRecord);

    if (op.stat == SUCCESS) {
        std::cout << "Record added successfully!" << std::endl;
    } else {
        std::cout << "Error adding record: " << op.error << std::endl;
    }
    db.save(); // save all unsaved changes
    db.close();
    return 0;
}

Getting all the values from a table

#include "qic.h"
#include <any>
#include <iostream>
#include <unordered_map>

int main() {
  data_base db;
  db.open_database("mydatabase.db");
  db.load_table("users"); // load the table, THIS IS THE MOST IMPORTANT STEP TO
                          // SAVE CHANGES

  auto users = db.get_values_from_table(
      "users"); // get the pointer of the vector containing all the table values
  for (int i = 0; i < users->size(); i++) {
    auto user = users->at(i);
    std::cout << "id : " << std::any_cast<int>(user["id"]) << std::endl;
    ;
    std::cout << "name : " << std::any_cast<std::string>(user["name"])
              << std::endl;
    std::cout << "active : " << std::any_cast<bool>(user["active"])
              << std::endl;
    std::cout << "balance : " << std::any_cast<double>(user["balance"])
              << std::endl;
  }
  db.save(); // save all unsaved changes
  db.close();
  return 0;
}

Last updated