create_table

Function: add_table

The add_table function is responsible for creating a new table within the currently opened database. A table in Q.I.C is represented by a file with the .table extension, and its structure is defined by a schema mapping column names to their corresponding data types.


Declaration

operation add_table(std::string name, std::unordered_map<std::string, data_type> content);

Parameters

  • name (std::string) The name of the table to be created. This will also determine the file name of the table (e.g., users.table for a table named users).

  • content (std::unordered_map<std::string, data_type>) A map defining the schema of the table. The key represents the column name, and the value represents the column's data type, which can be one of the following:

    • BOOL

    • INT

    • DOUBLE

    • FLOAT

    • STRING

Example

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

int main() {
    data_base db;

    // Open the database
    db.open_database("mydatabase.db");

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

    // Add a new table named "users"
    operation op = db.add_table("users", schema);

    // Check if the operation was successful
    if (op.stat == SUCCESS) {
        std::cout << "Table 'users' created successfully!" << std::endl;
    } else {
        std::cout << "Failed to create table: " << op.error << std::endl;
    }

    // Close the database
    db.close();

    return 0;
}

Last updated