C++ with SQLite3: Part 5: Encapsulating Database Objects

C++ with SQLite3

SQLite3 is the most-used SQL implementation in the world. It is a self-contained C library included in everything from portable devices to web browsers.

Note: This tutorial assumes some familiarity with using the command-line to create and compile files. It was written using a MacOS X machine and also assumes easy access to g++. (For Windows systems, consider using MinGW.)


Encapsulating Database Objects

In all the previous parts, SQLite3 code was simply listed as part of the other code. It was included as multiple uses of the sqlite3_exec() function. This is not very efficient.

In larger projects, all of the database functionality can be encapsulated, put into a single section or file, to help with managing it.

Most of the SQLite3 usage, in fact, can be broken up into its own functions.

Building Strings

Dealing with values passed to functions means building strings for it. To avoid issues with converting back and forth from char to string, the functions simply use char pointers. To help with building a string, the function asprintf() is used to allocate and build a new string based on what is given to it.

Calling Functions

Once the SQLite3 functionality has been broken up, it can be accessed through different function calls.