Welcome to our guide on using SQLite ORM, a light, header-only library designed to simplify database interactions in modern C++. This article will take you through installation, setup, and usage, helping you harness the power of this library with ease.
Installation
Using SQLite ORM is straightforward! Here are two methods to install the library:
- Using Package Manager: If you’re using a package manager like vcpkg, simply run:
vcpkg install sqlite-orm
- Building from Source: Clone the repository and build the project from source:
git clone https://github.com/fnc12/sqlite_orm.git sqlite_orm cd sqlite_orm cmake -B build cmake --build build --target install
Setting Up Your Database
Once installed, let’s set up your SQL database by mapping your C++ data model to the database schema. Imagine your C++ classes as the blueprint of a house, and the database schema as the actual house built according to that blueprint. They need to align perfectly for everything to work smoothly.
Here’s how we define our data model:
struct User {
int id;
std::string firstName;
std::string lastName;
int birthDate;
};
struct UserType {
int id;
std::string name;
};
And set up the database tables:
auto storage = make_storage(db.sqlite,
make_table("users",
make_column("id", &User::id, primary_key().autoincrement()),
make_column("first_name", &User::firstName),
make_column("last_name", &User::lastName),
make_column("birth_date", &User::birthDate)),
make_table("user_types",
make_column("id", &UserType::id, primary_key().autoincrement()),
make_column("name", &UserType::name, default_value("name_placeholder"))));
Notice how we map each class member to a specific column in the database table. This mapping allows seamless interaction between your data models and your SQL queries.
Performing CRUD Operations
CRUD stands for Create, Read, Update, and Delete. It encapsulates the essential functions you’ll want to use to manage your data.
Creating a Record
To create and insert a new user into your database, simply do the following:
User user {1, "John", "Doe", 664416000};
auto insertedId = storage.insert(user);
Reading a Record
To fetch a user’s details based on their ID:
try {
auto user = storage.get(insertedId);
} catch (std::system_error e) {
std::cerr << e.what() << std::endl;
}
Updating a Record
Updating is as simple as modifying the user object and calling update:
user.firstName = "Nicholas";
storage.update(user);
Deleting a Record
To delete a user, utilize the remove function:
storage.remove(insertedId);
Advanced Features
Prepared Statements
Prepared statements help prevent SQL injection and enhance performance. Here’s how you can execute one:
auto selectStatement = storage.prepare(select(*where(c(Visit::patientName) == "John")));
auto rows = storage.execute(selectStatement);
Aggregating Data
SQLite ORM allows easy aggregation functions:
auto userCount = storage.count();
Troubleshooting
If you encounter issues during development, consider these tips:
- Ensure that your classes are default constructible; this is crucial for database interactions.
- Double-check that all member variables are accessible, as they should not be const.
- Verify that SQLite3 is properly installed and compatible with your project.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
SQLite ORM makes database interactions in C++ a breeze with its user-friendly API and lightweight setup. Whether you're managing simple data or complex queries, SQLite ORM has the features to help you succeed!
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.