Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

sqlpp11

> 编程语言
Open source

A type safe SQL template library for C++

2.6K stars0 likes0 views
WebsiteGitHub

About

A type safe SQL template library for C++

sqlpp11

A type safe embedded domain specific language for SQL queries and results in C++.

[!IMPORTANT] [2025-06-29]
Please consider migrating to sqlpp23.
sqlpp11 will be mainained for the forseeable future, but new development is going to happen in sqlpp23, only.

Documentation is found in docs.

So what is this about?

SQL and C++ are both strongly typed languages. Still, most C/C++ interfaces to SQL are based on constructing queries as strings and on interpreting arrays or maps of strings as results.

sqlpp11 is a templated library representing an embedded domain specific language (EDSL) that allows you to

  • define types representing tables and columns,
  • construct type safe queries checked at compile time for syntax errors, type errors, name errors and even some semantic errors,
  • interpret results by iterating over query-specific structs with appropriately named and typed members.

This results in several benefits, e.g.

  • the library user operates comfortably on structs and functions,
  • the compiler reports many kinds of errors long before the code enters unit testing or production,
  • the library hides the gory details of string construction for queries and interpreting results returned by select calls.

The library supports both static and dynamic queries. The former offers greater benefit in terms of type and consistency checking. The latter makes it easier to construct queries in flight.

sqlpp11’s core is vendor-neutral. Specific traits of databases (e.g. unsupported or non-standard features) are handled by connector libraries. Connector libraries can inform the developer of missing features at compile time. They also interpret expressions specifically where needed. For example, the connector could use the operator|| or the concat method for string concatenation without the developer being required to change the statement.

Connectors for MariaDB, MySQL, PostgreSQL, sqlite3, sqlcipher are included in this repository.

The library is already used in production but it is certainly not complete yet. Feature requests, bug reports, contributions to code or documentation are most welcome.

Examples:

For the examples, lets assume you have a table class representing something like

CREATE TABLE foo (
    id bigint,
    name varchar(50),
    hasFun bool
);

And we assume to have a database connection object:

…

License:

sqlpp11 is distributed under the BSD 2-Clause License.

Status:

Branch / Compiler clang, gcc MSVC Test Coverage master develop

Additional information available:

Past talks about sqlpp11 and some coding concepts used within the library:

  • CppCast:
  • 2015-05-07: http://cppcast.com/2015/05/roland-bock/
  • CppCon:
  • 2015-09-24: Pruning Error Messages From Your C++ Template Code, with examples from sqlpp11
  • 2014-09-11: sqlpp11, An SQL Library Worthy Of Modern C++
  • Meeting C++:
  • 2014-12-05: sqlpp11, An EDSL For Type-Safe SQL In C++11
  • MUC++:
  • 2014-02-27: Selected C++11 Template Toffees From sqlpp11, Part1, Part2, Part 3, Part 4

Requirements:

Compiler: sqlpp11 makes heavy use of C++11 and requires a recent compiler and STL. The following compilers are known to compile the test programs:

  • clang-3.4+ on Ubuntu-12.4
  • g++-4.8+ on Ubuntu-12.4
  • g++-4.8+ on cygwin 64bit
  • g++-4.9+ on Debian Unstable
  • Xcode-7 on OS X
  • MSVC 2015 Update 1 on Windows Server 2012

Database Connector: sqlpp11 requires a certain api in order to connect with the database, see database/api.h.

This repository includes the following connectors:

  • MySQL
  • MariaDB
  • SQLite3
  • SQLCipher
  • PostgreSQL

Other connectors can be found here:

  • ODBC: https://github.com/Erroneous1/sqlpp11-connector-odbc (experimental)

Date Library: sqlpp11 requires Howard Hinnant’s date library for date and date_time data types. By default, sqlpp11 uses FetchContent to pull the library automatically in the project. If you want to use an already installed version of the library with find_package, set USE_SYSTEM_DATE option to ON.

Build and Install

Note: Depending on how you use the lib, you might not need to install it (see Basic Usage)

Build from Source:

Download and unpack the latest release from https://github.com/rbock/sqlpp11/releases or clone the repository. Inside the directory run the following commands:

cmake -B build
cmake --build build --target install

The last step will build the library and install it system wide, therefore it might need admins rights.

By default only the core library will be installed. To also install connectors set the appropriate variable to ON:

  • BUILD_MYSQL_CONNECTOR
  • BUILD_MARIADB_CONNECTOR
  • BUILD_POSTGRESQL_CONNECTOR
  • BUILD_SQLITE3_CONNECTOR
  • BUILD_SQLCIPHER_CONNECTOR

The library will check if all required dependencies are installed on the system. If connectors should be installed even if the dependencies are not yet available on the system, set DEPENDENCY_CHECK to OFF.

Example: Install the core library, sqlite3 connector and postgresql connector. Don’t check if the dependencies such as Sqlite3 are installed and don’t build any tests:

cmake -B build -DBUILD_POSTGRESQL_CONNECTOR=ON -DBUILD_SQLITE3_CONNECTOR=ON -DDEPENDENCY_CHECK=OFF -DBUILD_TESTING=OFF
cmake --build build --target install

Install via Homebrew (MacOS):

brew install marvin182/zapfhahn/sqlpp11

Some connectors can be installed with the formula. See brew info marvin182/zapfhahn/sqlpp11 for available options.

Build via vcpkg:

You can download and install sqlpp11 using the vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install 'sqlpp11[mysql]' # or other database feature

The sqlpp11 port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.

Basic usage:

Use with cmake: The library officially supports two ways how it can be used with cmake. You can find examples for both methods in the examples folder.

  1. FetchContent (Recommended, no installation required)
  2. FindPackage (installation required, see above)

Both methods will provide the sqlpp11::sqlpp11 target as well as targets for each connector:

  • sqlpp11::mysql
  • sqlpp11::mariadb
  • sqlpp11::sqlite3
  • sqlpp11::sqlcipher
  • sqlpp11::postgresql

These targets will make sure all required dependencies are available and correctly linked and include directories are set correctly.

Create DDL files:

mysql: 'show create table MyDatabase.MyTable' #or
mysqldump --no-data MyDatabase > MyDatabase.sql

Create headers for them with provided Python script:

%sqlpp11_dir%/scripts/ddl2cpp ~/temp/MyTable.ddl  ~/temp/MyTable %DatabaseNamespaceForExample%

In case you’re getting notes about unsupported column type consider:

  • Take a look at the other datatypes in sqlpp11/data_types. They are not hard to implement.
  • Use the --datatype-file command line argument as described below.

Include generated header (MyTable.h), that’s all.

If you prefer Ruby over Python, you might want to take a look at https://github.com/douyw/sqlpp11gen

Unsupported column types:

Map unsupported column types to supported column types with a csv file:

One can use the --datatype-file command line argument for the ddl2cpp script to map unsupported column types to supported column types.

The format of the csv file is:

<dataType>, <col_type1>, <col_type2>
<dataType>, <col_type3>

Where <dataType> is one or more of the following internal types:

  • Boolean
  • Integer
  • Serial
  • FloatingPoint
  • Text
  • Blob
  • Date
  • DateTime
  • Time

Example:

Boolean, one_or_zero
Text, url, uuid

Contact:

  • Issues at https://github.com/rbock/sqlpp11/issues
  • email at rbock at eudoxos dot de

GitHub Issues· 0 open

View all on GitHub

No open issues yet, or sync has not completed.

Highlights

  • •define types representing tables and columns,
  • •construct type safe queries checked at compile time for syntax errors, type errors, name errors and even some semantic errors,
  • •interpret results by iterating over query-specific structs with appropriately named and typed members.
  • •the library user operates comfortably on structs and functions,
  • •the compiler reports many kinds of errors long before the code enters unit testing or production,
  • •the library hides the gory details of string construction for queries and interpreting results returned by select calls.
  • •CppCast:
  • •2015-05-07: http://cppcast.com/2015/05/roland-bock/
  • •2015-09-24: Pruning Error Messages From Your C++ Template Code, with examples from sqlpp11
  • •2014-09-11: sqlpp11, An SQL Library Worthy Of Modern C++

> Tags

C++

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言