get_values_from_table

Function: get_values_from_table

The get_values_from_table function retrieves all records (rows) stored in memory for a specified table. These records are returned as a vector of key-value pairs, where the keys represent column names and the values are stored using std::any.

WARNING: use the load_table function before getting the values from the table as that function loads into memory all the values, without running this function the vector will be empty


Declaration

std::vector<std::unordered_map<std::string, std::any>>* get_values_from_table(std::string table);

Parameters

  • table (std::string) The name of the table from which values should be retrieved. The table must already exist and be loaded in memory.

Return Value

  • std::vector<std::unordered_map<std::string, std::any>>* A pointer to the in-memory vector containing all records for the specified table:

    • Each element in the vector represents a single record (or row).

    • Each record is a map where:

      • The key is the column name.

      • The value is the data stored in that column, wrapped in std::any.

Functionality

  1. Database Validation The function does not explicitly validate whether the database is open or whether the table exists. It directly accesses the tables attribute of the data_base class, relying on the integrity of this map.

  2. Access Records in Memory It retrieves the vector of records associated with the specified table from the tables map.

  3. Pointer Return The function returns a pointer to the in-memory vector, allowing efficient access to the data for further processing.

Example

Here’s an example demonstrating how to use the get_values_from_table function:

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

int main() {
  data_base db;
  db.open_database("mydatabase.db");
  db.load_table("COMPANY"); // loads the table in memory

  auto table =
      db.get_values_from_table("COMPANY"); // if we run this function without
                                           // the load table function before it
                                           // wont have all the tables in memory
  table->clear(); // by clearing this list and saving the db we can clear the
                  // table

  db.save();
  std::cin >> new char;
  db.close();
  return 0;
}

Last updated