SQLite Containers
Loading...
Searching...
No Matches
example-map-blob.cpp
Go to the documentation of this file.
2#include <iostream>
3#include <map>
4#include <vector>
5#include <sstream>
6
7// Helper function to print contents of std::map
8template <typename MapType>
9void print_map(const MapType& map, const std::string& header) {
10 std::cout << header << std::endl;
11 for (const auto& pair : map) {
12 std::cout << "Key: " << pair.first << ", Value: ";
13 for (char c : pair.second) {
14 std::cout << c;
15 }
16 std::cout << std::endl;
17 }
18}
19
20int main() {
21 // Database configuration creation
23 config.db_path = "example_vector.db"; // Path to the database
24
25 // Creating KeyValueDB instance for int keys and std::vector<char> values
27 map_db.connect(); // Connect to the database
28
29 // Inserting several key-value pairs
30 map_db.insert(1, {'a', 'b', 'c'});
31 map_db.insert(2, {'d', 'e', 'f'});
32 map_db.insert(3, {'g', 'h', 'i'});
33
34 // Find and print the value for key 2
35 std::vector<char> value;
36 if (map_db.find(2, value)) {
37 std::cout << "Found value for key 2: ";
38 for (char c : value) {
39 std::cout << c;
40 }
41 std::cout << std::endl;
42 } else {
43 std::cout << "Key 2 not found." << std::endl;
44 }
45
46 // Load database contents into a std::map using load method
47 std::map<int, std::vector<char>> my_map;
48 map_db.load(my_map);
49 print_map(my_map, "Contents of my_map after load:");
50
51 // Retrieve all key-value pairs directly from the database
52 std::map<int, std::vector<char>> all_entries = map_db.retrieve_all<std::map>();
53 print_map(all_entries, "Contents of the database using retrieve_all:");
54
55 // Removing a key-value pair with key 3
56 map_db.remove(3);
57 all_entries = map_db.retrieve_all<std::map>();
58 print_map(all_entries, "Contents of the database after removing key 3:");
59
60 // Inserting a new key-value pair
61 map_db.insert(4, {'j', 'k', 'l'});
62 all_entries = map_db.retrieve_all<std::map>();
63 print_map(all_entries, "Contents of the database after inserting key 4:");
64
65 // Append the contents of std::map to the database
66 my_map[5] = {'m', 'n', 'o'};
67 map_db.append(my_map); // Append data from my_map to the database
68 all_entries = map_db.retrieve_all<std::map>();
69 print_map(all_entries, "Contents of the database after append:");
70
71 // Example of reconciling (syncing) std::map contents with the database
72 my_map.erase(5); // Remove a key-value pair from the map
73 my_map[6] = {'p', 'q', 'r'};
74 map_db.reconcile(my_map); // Synchronize database with map changes
75 all_entries = map_db.retrieve_all<std::map>();
76 print_map(all_entries, "Contents of the database after reconcile:");
77
78 // Using assignment operator for synchronization (same as reconcile)
79 my_map.erase(6); // Remove a key-value pair from the map
80 my_map[7] = {'s', 't', 'u'};
81 map_db = my_map; // Use assignment operator to sync with the database
82 all_entries = map_db.retrieve_all<std::map>();
83 print_map(all_entries, "Contents of the database after using operator= to reconcile:");
84
85 // Clearing the database
86 map_db.clear();
87 all_entries = map_db.retrieve_all<std::map>();
88 if (all_entries.empty()) {
89 std::cout << "Database is empty after clear." << std::endl;
90 } else {
91 print_map(all_entries, "Contents of the database after clear:");
92 }
93
94 return 0;
95}
Declaration of the KeyValueDB 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.
void clear()
Clears all key-value pairs from the database.
void remove(const KeyT &key)
Removes a key-value pair from the database.
void reconcile(const ContainerT< KeyT, ValueT > &container)
Reconciles the database with the container.
void insert(const KeyT &key, const ValueT &value)
Inserts a key-value pair into the database.
ContainerT< KeyT, ValueT > retrieve_all()
Retrieves all key-value pairs.
bool find(const KeyT &key, ValueT &value)
Finds a value by key.
void append(const ContainerT< KeyT, ValueT > &container)
Appends data to the database.
void load(ContainerT< KeyT, ValueT > &container)
Loads data from the database into the container.
void print_map(const MapType &map, const std::string &header)
int main()