odbc-cpp-wrapper is an object-oriented C++-wrapper of the ODBC API. It takes care of
- managing the lifetime of ODBC resources,
- allocating and managing resources needed for ODBC operations and
- converting ODBC errors to exceptions and throwing them.
The odbc-cpp-wrapper API attempts to make usage of ODBC as simple as possible. The API was designed to make wrong usage almost impossible and to ensure proper object lifetime management.
odbc-cpp-wrapper was originally developed for exchanging spatial data with databases. It focuses on batch operations of variable-sized data, which is not very well supported by other ODBC wrappers.
To build odbc-cpp-wrapper you need
- A C++-11-standard-compliant compiler
- The Git command line client
- CMake 3.12 or newer
On Linux platforms you additionally need
To generate the API's documentation, you need
-
Clone the repository:
git clone https://github.com/SAP/odbc-cpp-wrapper.git -
Create a build directory and change to it:
mkdir odbc-cpp-wrapper/build && cd odbc-cpp-wrapper/build -
Create the makefiles with CMake:
cmake .. -
Build the library:
make -j <number of parallel build jobs>By default, the build will create a static library
libodbccpp.a. To build a shared library instead, configure CMake with-DBUILD_SHARED_LIBS=ON:cmake -DBUILD_SHARED_LIBS=ON .. -
To build the documentation (optional):
make docThe mainpage of the documentation can be found at
doc/html/index.html. -
Install the library:
sudo make installThis will install the library and header files. CMake will install them to
usr/local/libandusr/local/includeby default. If you prefer different locations, you can set CMake's install prefix to a different path. See https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html for details.
-
Clone the repository:
git clone https://github.com/SAP/odbc-cpp-wrapper.git -
Create a build directory and change to it:
mkdir odbc-cpp-wrapper\build && cd odbc-cpp-wrapper\build
-
Generate a Visual Studio solution
cmake ..You can then open the
odbccpp.slnfile and build the desired targets in Visual Studio.
-
Start the Visual Studio Native Tools Command Prompt for the desired target and change the directory to the build directory. Create the makefiles for nmake:
cmake -G "NMake Makefiles" ..Optionally you can use CMAKE_BUILD_TYPE to define if you'd like to build a Debug or Release build. See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html for details.
-
Build the library:
nmakeBy default, the build will create a static library
odbccpp.lib. To build a dynamic link library instead, configure CMake with-DBUILD_SHARED_LIBS=ON:cmake -G "NMake Makefiles" -DBUILD_SHARED_LIBS=ON .. -
Build the documentation (optional):
nmake docThe mainpage of the documentation can be found at
doc\html\index.html. -
Install the library (optional):
nmake installThis will install the library and header files. CMake will install them to
C:\Program Files\odbccppby default. If you prefer a different location, you can set CMake's install prefix to a different path. See https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html for details.
You can link against either the shared/dynamic or the static library, depending on how you configured the build with BUILD_SHARED_LIBS.
If you are using CMake to build your project, it suffices to link against the odbccpp target.
If you are not using CMake and are linking against the static library, you have to additionally define ODBC_STATIC when compiling your code.
Usage of the library should be pretty straight-forward if you are familiar with ODBC and/or other database connectors.
The following code gives an example how working with odbc-cpp-wrapper looks like. It connects to a database, batch inserts two rows and executes a query.
#include <iostream>
#include <odbc/Connection.h>
#include <odbc/Environment.h>
#include <odbc/Exception.h>
#include <odbc/PreparedStatement.h>
#include <odbc/ResultSet.h>
int main()
{
try
{
odbc::EnvironmentRef env = odbc::Environment::create();
odbc::ConnectionRef conn = env->createConnection();
conn->connect("DSN", "user", "pass");
conn->setAutoCommit(false);
odbc::PreparedStatementRef psInsert =
conn->prepareStatement("INSERT INTO TAB (ID, DATA) VALUES (?, ?)");
psInsert->setInt(1, 101);
psInsert->setCString(2, "One hundred one");
psInsert->addBatch();
psInsert->setInt(1, 102);
psInsert->setCString(2, "One hundred two");
psInsert->addBatch();
psInsert->executeBatch();
conn->commit();
odbc::PreparedStatementRef psSelect =
conn->prepareStatement("SELECT ID, DATA FROM TAB WHERE ID > ?");
psSelect->setInt(1, 100);
odbc::ResultSetRef rs = psSelect->executeQuery();
while (rs->next())
{
std::cout << rs->getInt(1) << ", " << rs->getString(2) << std::endl;
}
}
catch (const odbc::Exception& e)
{
std::cerr << e.what() << std::endl;
}
}If you experience issues with using the library, please file a report in the GitHub bug tracking system.
Copyright 2019-2021 SAP SE or an SAP affiliate company and odbc-cpp-wrapper contributors. Please see our LICENSE for copyright and license information. Please note the GPLv2 Combination Exception for the Apache 2 License! Detailed information including third-party components and their licensing/copyright information is available via the REUSE tool.