SQL++11 ORM In a Nutshell
So, you’ve been wrestling with SQL queries in your C++ code, manually handling connections, preparing statements, and juggling result sets like a circus act. Enter sqlpp11
, the C++ ORM (Object-Relational Mapping) library that makes SQL in C++ less painful and a lot more fun (well, as fun as SQL can get).
In this guide, we’re going to break down sqlpp11
, how it works, and why you might want to use it.
What is sqlpp11
?
sqlpp11
is a type-safe SQL query library for C++11 and beyond. It’s not exactly a full-blown ORM like Hibernate in Java or Entity Framework in C#. Instead, it provides an intuitive way to construct SQL queries using C++ syntax while maintaining type safety.
The main perks:
- No more string-based SQL queries – You can write queries in C++ and let the compiler check them for you.
- Supports multiple backends – Works with MySQL, SQLite, PostgreSQL, and more.
- Header-only – No need to link against a library; just include and go.
- Composable Queries – Queries can be built step by step in a clean, functional style.
Installing sqlpp11
Before we start writing queries, we need to install sqlpp11
.
1. Install Dependencies
You’ll need:
- A C++11-compatible compiler (GCC, Clang, MSVC, etc.)
- CMake for building
- Database-specific connectors (e.g.,
sqlpp11-connector-mysql
for MySQL)
For example, installing it with MySQL support might look like this:
1
| sudo apt-get install libmysqlclient-dev
|
2. Install sqlpp11
1
2
3
4
5
6
| git clone https://github.com/rbock/sqlpp11.git
cd sqlpp11
mkdir build && cd build
cmake ..
make
sudo make install
|
You’ll also need a database-specific connector, such as sqlpp11-connector-mysql
:
1
2
3
4
5
6
| git clone https://github.com/rbock/sqlpp11-connector-mysql.git
cd sqlpp11-connector-mysql
mkdir build && cd build
cmake ..
make
sudo make install
|
Writing Queries with sqlpp11
Let’s jump into some code and see how sqlpp11
helps us avoid SQL injection disasters and runtime query errors.
Setting Up a Database Connection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| #include <sqlpp11/sqlpp11.h>
#include <sqlpp11/mysql/mysql.h>
#include "my_database.h" // This is your table definition
int main() {
sqlpp::mysql::connection_config config;
config.user = "root";
config.password = "password";
config.database = "test_db";
sqlpp::mysql::connection db(config);
std::cout << "Connected to MySQL!" << std::endl;
return 0;
}
|
Defining a Table
Instead of writing raw SQL table definitions, sqlpp11
uses C++ structs:
1
2
3
4
5
6
7
8
9
10
| #include <sqlpp11/sqlpp11.h>
#include <sqlpp11/table.h>
namespace my_database {
struct Users : sqlpp::table_t<Users,
sqlpp::column_t<int, sqlpp::primary_key, sqlpp::auto_increment, sqlpp::not_null>,
sqlpp::column_t<std::string, sqlpp::not_null>> {
struct _alias_t { static constexpr const char _literal[] = "users"; };
};
}
|
Inserting Data
1
2
| my_database::Users users;
db(insert_into(users).set(users.name = "Alice"));
|
Querying Data
1
2
3
| for (const auto& row : db(select(all_of(users)).from(users).where(users.id == 1))) {
std::cout << "User: " << row.name << std::endl;
}
|
Updating Data
1
| db(update(users).set(users.name = "Bob").where(users.id == 1));
|
Deleting Data
1
| db(remove_from(users).where(users.id == 1));
|
Comparing sqlpp11
with Other ORMs
Feature | sqlpp11 | ODB | Hibernate (Java) | Entity Framework (C#) |
---|
Type-Safe Queries | ✅ | ✅ | ❌ | ✅ |
Supports Multiple Databases | ✅ | ✅ | ✅ | ✅ |
Header-Only | ✅ | ❌ | ❌ | ❌ |
Uses C++ Templates | ✅ | ✅ | ❌ | ❌ |
Requires Code Generation | ❌ | ✅ | ✅ | ✅ |
Key Ideas
Key Idea | Summary |
---|
Type Safety | sqlpp11 ensures queries are checked at compile time. |
Composability | Build queries dynamically with C++ syntax. |
Header-Only | No extra linking required, just include and go. |
Database Support | Works with MySQL, SQLite, PostgreSQL, etc. |
Installation | Requires CMake and a database-specific connector. |
References