add_value

Function: add_value

The add_value function allows you to add a new record (or row) to a specific table in the database. Each record is stored as a collection of key-value pairs, where the key is the column name and the value is the data to be inserted.


Declaration

operation add_value(std::string table, std::unordered_map<std::string, std::any> values);

Parameters

  • table (std::string) The name of the table where the new record should be added. This table must already exist in the database.

  • values (std::unordered_map<std::string, std::any>) A map containing the values to insert into the table. Each key corresponds to a column name in the table's schema, and each value must match the column's data type.

Return Value

  • operation An object containing:

    • stat: Indicates whether the operation was SUCCESS or FAILED.

    • error: Provides a description of the error if the operation fails.

Functionality

  1. Checks if the Database is Open The function verifies that the database is open. If the database is not open, the operation fails with an appropriate error message.

  2. Validates Table Existence The function checks if the specified table exists in the database. If the table is not found, the operation fails with an error message stating "Table does not exist."

  3. Adds the Record to the Table If the table is found, the function appends the new record (i.e., the values map) to the in-memory collection of records for that table.

  4. Returns the Result If the record is successfully added, the operation status is set to SUCCESS, and an informational message ("New elements added to the table") is returned. Otherwise, an error message is provided.

Example

Here’s a simple example of how to use the add_value function:

Last updated