# Quick start

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.

```cpp
#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

```cpp
#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

<pre class="language-cpp"><code class="lang-cpp">#include "qic.h"
#include &#x3C;iostream>
#include &#x3C;unordered_map>
#include &#x3C;any>

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

    // Create a new record
    std::unordered_map&#x3C;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 &#x3C;&#x3C; "Record added successfully!" &#x3C;&#x3C; std::endl;
    } else {
        std::cout &#x3C;&#x3C; "Error adding record: " &#x3C;&#x3C; op.error &#x3C;&#x3C; std::endl;
    }
<strong>    db.save(); // save all unsaved changes
</strong>    db.close();
    return 0;
}
</code></pre>

Getting all the values from a table

```cpp
#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;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hrodebert.gitbook.io/qic-database-ver-1.0.0/getting-started/quick-start.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
