add_value
Function: add_value
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 wasSUCCESS
orFAILED
.error
: Provides a description of the error if the operation fails.
Functionality
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.
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."
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.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:
#include "qic.h"
#include <iostream>
#include <unordered_map>
int main() {
data_base db; // Step 1: Create an instance of the database class.
// Step 2: Open the database file "mydatabase.db".
// This will initialize the database and prepare it for operations.
db.open_database("mydatabase.db");
// Step 3: Create a new table named "COMPANY" with the specified schema.
auto o = db.add_table("COMPANY", std::unordered_map<std::string, data_type>{
{"ID", INT}, // Integer column for employee IDs.
{"NAME", STRING}, // String column for employee names.
{"AGE", INT}, // Integer column for employee ages.
{"ADDRESS", STRING}, // String column for employee addresses.
{"SALARY", DOUBLE}}); // Double column for employee salaries.
// Check if the table creation was successful.
if (o.stat == FAILED) {
// If the operation failed, output the error message.
std::cout << "error: " << o.error << std::endl;
} else {
// If successful, indicate that the table was created.
std::cout << "created the table" << std::endl;
}
// Step 4: Add a record (row) to the "COMPANY" table.
o = db.add_value("COMPANY",
std::unordered_map<std::string, std::any>{
{"ID", std::any(0)}, // Employee ID 0.
{"NAME", std::any(std::string("Hrodebert"))}, // Name "Hrodebert".
{"AGE", std::any(120)}, // Age 120.
{"ADDRESS", std::any(std::string("not known"))}, // Address "not known".
{"SALARY", std::any((double)0)}}); // Salary 0.0.
// Check if the record insertion was successful.
if (o.stat == FAILED) {
// If the operation failed, output the error message.
std::cout << "error: " << o.error << std::endl;
} else {
// If successful, indicate that the record was added.
std::cout << "added the tables" << std::endl;
}
// Step 5: Save the changes to ensure that the newly added table and data
// persist in the database file.
db.save();
// Step 6: Wait for user input before closing the database.
// (The `new char` input here serves no functional purpose; consider replacing it with something meaningful if needed.)
std::cin >> new char;
// Step 7: Close the database to finalize and clean up temporary files.
db.close();
return 0; // End of the program.
}
Last updated