Featured image of post SOCI ORM In a Nutshell

SOCI ORM In a Nutshell

C++ ORM

SOCI ORM In a Nutshell

If you’ve ever looked at your C++ code and thought, “Wow, managing database queries is like herding cats,” then SOCI might just be your new best friend.

SOCI (pronounced “soh-chee,” like the Russian city) is a lightweight, C++ database access library that acts as an ORM (Object-Relational Mapping) but still gives you plenty of control. It’s like an ORM but without all the overbearing “let me do everything for you” attitude.


Why Use SOCI?

1. It’s Lightweight

Unlike heavyweight ORMs that bloat your application like a fast-food meal, SOCI keeps things simple and efficient.

2. It Supports Multiple Databases

SOCI plays nice with a variety of databases, including:

  • PostgreSQL
  • MySQL
  • SQLite
  • Oracle
  • Microsoft SQL Server

3. It’s SQL-Friendly

You write actual SQL queries but in a way that’s safe and easy to manage within C++.

4. It’s Easy to Learn

You don’t need a Ph.D. in ORM theology to use it. A few lines of code, and you’re rolling.


Setting Up SOCI

Before diving in, install SOCI with:

For Debian-based Linux:

1
sudo apt-get install libSOCI-dev

For macOS using Homebrew:

1
brew install SOCI

For Windows, you’ll need to build it from source. But hey, you’re a C++ developer—nothing scares you.


Basic Usage

Connecting to a Database

Let’s start with the most fundamental step: connecting to a database.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <soci/soci.h>
#include <soci/sqlite3/soci-sqlite3.h>

using namespace soci;

int main() {
    try {
        session sql(sqlite3, "my_database.db");
        std::cout << "Connected to database!" << std::endl;
    } catch (const soci::soci_error &e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }
    return 0;
}

Boom! You’re connected to SQLite. Replace sqlite3 with postgresql, mysql, or oracle to switch databases.


Querying the Database

Now let’s fetch some data. Assume we have a users table.

1
2
3
4
5
std::string name;
int age;
sql << "SELECT name, age FROM users WHERE id = 1", into(name), into(age);

std::cout << "User: " << name << " (" << age << " years old)" << std::endl;

Simple, right? No convoluted ORM magic—just a clean and readable query.


Inserting Data

Adding a new user is just as easy:

1
2
3
4
std::string new_name = "Alice";
int new_age = 30;
sql << "INSERT INTO users (name, age) VALUES (:name, :age)",
    use(new_name), use(new_age);

Placeholders (:name and :age) keep things safe and prevent SQL injection—because nobody likes a hacked database.


Updating Data

Updating records is a breeze:

1
2
3
4
int user_id = 1;
std::string new_name = "Bob";
sql << "UPDATE users SET name = :name WHERE id = :id",
    use(new_name), use(user_id);

SOCI makes SQL feel right at home in C++.


Deleting Data

Deleting a user? No problem:

1
2
int user_id = 2;
sql << "DELETE FROM users WHERE id = :id", use(user_id);

That’s it! Clean and simple.


SOCI vs Other ORMs

Let’s put SOCI head-to-head against some other C++ database solutions:

FeatureSOCIODBQt SQLRaw SQL
Lightweight
Easy to Use
Multiple DB Support
ORM Features
SQL Control

SOCI is the best of both worlds—structured, safe SQL handling without heavy ORM baggage.


Key Ideas

TopicSummary
What is SOCI?A lightweight C++ ORM alternative that simplifies database handling.
Why SOCI?SQL-friendly, lightweight, supports multiple databases, and is easy to learn.
Connecting to DBUses session sql(database, connection_string) to manage connections.
Querying DataUses parameterized queries to fetch data safely.
CRUD OperationsSupports insert, update, and delete operations efficiently.
ComparisonLighter than ODB, more SQL-friendly than Qt SQL, and safer than raw SQL.

References