SQLite Containers
Loading...
Searching...
No Matches
example-map-set.cpp
Go to the documentation of this file.
1#include <iostream>
3#include <map>
4#include <set>
5
6// Function to print the contents of a map with std::set as values
7template <typename MapType>
8void print_map_with_set(const MapType& map, const std::string& header) {
9 std::cout << header << std::endl;
10 for (const auto& pair : map) {
11 if (!pair.second.empty()) {
12 for (const auto& item : pair.second) {
13 std::cout << "Key: " << pair.first << " -> Value: " << item << std::endl;
14 }
15 } else {
16 std::cout << "Key: " << pair.first << " has an empty set." << std::endl;
17 }
18 }
19}
20
21int main() {
22 try {
23 // Create database configuration
25 config.db_path = "example-map-set.db"; // Path to the database
26
27 // Create KeyMultiValueDB instance for int keys and std::set<int> values
29 key_value_db.connect(); // Connect to the database
30
31 // Clear the table for a fresh start
32 key_value_db.clear();
33
34 // Create a std::map with std::set as values
35 std::map<int, std::set<int>> map_with_set_pairs = {
36 {3, {1, 2}},
37 {1, {}}, // Empty set for key 1
38 };
39
40 // Append the contents of the std::map with std::set values to the database
41 std::cout << "Appending data to the database using reconcile..." << std::endl;
42 key_value_db.reconcile(map_with_set_pairs); // Synchronize the map with the database
43
44 // Retrieve all key-value pairs from the database and print them
45 auto retrieved_map_with_set_pairs = key_value_db.retrieve_all<std::map, std::set>();
46 print_map_with_set(retrieved_map_with_set_pairs, "Key-value pairs in database after reconcile:");
47
48 // Inserting a new key-value pair
49 key_value_db.insert(4, 3); // Key 4 -> Set with value 3
50 key_value_db.insert(4, 5); // Add another value to the set for key 4
51
52 // Retrieve all key-value pairs again and print them
53 retrieved_map_with_set_pairs = key_value_db.retrieve_all<std::map, std::set>();
54 print_map_with_set(retrieved_map_with_set_pairs, "Key-value pairs in database after inserting new values:");
55
56 } catch (const sqlite_containers::sqlite_exception &e) {
57 std::cerr << "SQLite error: " << e.what() << std::endl;
58 } catch (const std::exception &e) {
59 std::cerr << "Error: " << e.what() << std::endl;
60 }
61
62 return 0;
63}
Template class for managing key-value pairs in a SQLite database.
void connect()
Connects to the database using the current configuration. Initializes a connection to the database by...
Definition BaseDB.hpp:53
Configuration class for SQLite database settings.
Definition Config.hpp:11
std::string db_path
Path to the SQLite database file.
Definition Config.hpp:13
Template class for managing key-value pairs in a SQLite database, where each key can map to multiple ...
ContainerT< KeyT, ValueT > retrieve_all(const TransactionMode &mode)
Retrieves all key-value pairs from the database with a transaction.
void insert(const KeyT &key, const ValueT &value, const TransactionMode &mode)
Inserts a key-value pair into the database with a transaction.
void clear(const TransactionMode &mode)
Clears all key-value pairs from the database with a transaction.
void reconcile(const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
Reconciles the content of the container with the database with a transaction.
Exception class for SQLite errors.
Definition Utils.hpp:27
void print_map_with_set(const MapType &map, const std::string &header)
int main()