SQLite Containers
Loading...
Searching...
No Matches
example-set.cpp
Go to the documentation of this file.
2#include <iostream>
3#include <set>
4#include <list>
5#include <vector>
6
7int main() {
8 try {
9 // Create database configuration
11 config.db_path = "example-set.db";
12
13 // Create KeyDB instance
14 sqlite_containers::KeyDB<int> key_db(config);
15 key_db.connect();
16
17 // Clear the table for a fresh start
18 key_db.clear();
19
20 // Create a std::set with keys
21 std::set<int> keys = {1, 2, 3, 4, 5};
22
23 // Append the contents of the std::set to the database
24 key_db.append(keys);
25
26 // Retrieve all keys from the database and print them
27 std::set<int> retrieved_keys_set = key_db.retrieve_all<std::set>();
28 std::cout << "Keys in database after append: ";
29 for (const auto& key : retrieved_keys_set) {
30 std::cout << key << " ";
31 }
32 std::cout << std::endl;
33
34 // Check the number of keys in the database
35 std::cout << "Number of keys in the database: " << key_db.count() << std::endl;
36
37 // Check if the database is empty
38 std::cout << "Is the database empty? " << (key_db.empty() ? "Yes" : "No") << std::endl;
39
40 // Insert a new key
41 key_db.insert(6);
42
43 // Retrieve all keys and print them as a std::list
44 std::list<int> retrieved_keys_list = key_db.retrieve_all<std::list>();
45 std::cout << "Keys in database after insert: ";
46 for (const auto& key : retrieved_keys_list) {
47 std::cout << key << " ";
48 }
49 std::cout << std::endl;
50
51 // Check if the key exists in the database
52 if (key_db.find(6)) {
53 std::cout << "Key 6 found in the database." << std::endl;
54 } else {
55 std::cout << "Key 6 not found in the database." << std::endl;
56 }
57
58 // Check if a non-existing key is found
59 if (key_db.find(10)) {
60 std::cout << "Key 10 found in the database." << std::endl;
61 } else {
62 std::cout << "Key 10 not found in the database." << std::endl;
63 }
64
65 // Remove a key
66 key_db.remove(3);
67
68 // Retrieve all keys from the database after removal and print them
69 std::vector<int> retrieved_keys_vector = key_db.retrieve_all<std::vector>();
70 std::cout << "Keys in database after removing key 3: ";
71 for (const auto& key : retrieved_keys_vector) {
72 std::cout << key << " ";
73 }
74 std::cout << std::endl;
75
76 // Example of using operator= to synchronize with the contents of a new std::set
77 std::set<int> new_keys = {10, 20, 30};
78 key_db = new_keys; // Using operator= to synchronize
79 std::vector<int> keys_after_assignment = key_db.retrieve_all<std::vector>();
80 std::cout << "Keys in database after using operator= with new set: ";
81 for (const auto& key : keys_after_assignment) {
82 std::cout << key << " ";
83 }
84 std::cout << std::endl;
85
86 // Example of using operator() to load keys from the database into a std::list
87 std::list<int> keys_loaded_with_operator = key_db.template operator()<std::list>();
88 std::cout << "Keys loaded using operator(): ";
89 for (const auto& key : keys_loaded_with_operator) {
90 std::cout << key << " ";
91 }
92 std::cout << std::endl;
93
94 // Check the number of keys in the database after operations
95 std::cout << "Number of keys in the database: " << key_db.count() << std::endl;
96
97 // Check if the database is empty after operations
98 std::cout << "Is the database empty? " << (key_db.empty() ? "Yes" : "No") << std::endl;
99
100 } catch (const sqlite_containers::sqlite_exception &e) {
101 std::cerr << "SQLite error: " << e.what() << std::endl;
102 } catch (const std::exception &e) {
103 std::cerr << "Error: " << e.what() << std::endl;
104 }
105 return 0;
106}
Declaration of the KeyDB class for managing keys 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 keys in a SQLite database.
Definition KeyDB.hpp:17
std::size_t count() const
Returns the number of keys in the database.
Definition KeyDB.hpp:225
bool empty() const
Checks if the database is empty (no keys present).
Definition KeyDB.hpp:233
bool find(const KeyT &key)
Finds if a key exists in the database.
Definition KeyDB.hpp:217
ContainerT< KeyT > retrieve_all()
Retrieves all keys from the database.
Definition KeyDB.hpp:111
void append(const ContainerT< KeyT > &container)
Appends the content of the container to the database.
Definition KeyDB.hpp:152
void insert(const KeyT &key)
Inserts a key into the database.
Definition KeyDB.hpp:208
void clear()
Clears all keys from the database.
Definition KeyDB.hpp:247
void remove(const KeyT &key)
Removes a key from the database.
Definition KeyDB.hpp:240
Exception class for SQLite errors.
Definition Utils.hpp:27
int main()