SQLite for C++: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is SQLite and why is it needed in C++?

\n\n

SQLite is a lightweight, embedded relational database that does not require a separate server process. Unlike MySQL or PostgreSQL, SQLite works directly with a database file on disk, making it an ideal choice for desktop applications, mobile devices, embedded systems, and small web projects.

\n\n

For the C++ language, there are several ways to work with SQLite. The most popular and stable is using the official sqlite3 library (C API), which is provided as a set of C functions. However, for more convenient work in C++, wrappers such as SQLiteCpp or sqlite_modern_cpp are often used. In this article, we will focus on direct use of the C API (sqlite3 library), as it provides maximum control and understanding of the underlying processes.

\n\n

Key advantages of SQLite:

\n
    \n
  • Zero configuration: no need to install and configure a server.
  • \n
  • Portability: the database is a single file.
  • \n
  • Reliability: support for transactions (ACID).
  • \n
  • Speed: for most operations, it is faster than client-server databases.
  • \n
  • Size: the library takes up less than 600 KB.
  • \n
\n\n

Installing SQLite for C++

\n\n

Installation depends on your operating system and compiler. We will cover three main methods.

\n\n

1. Installation via package manager (Linux/macOS)

\n\n

Ubuntu/Debian:

\n
sudo apt-get update\nsudo apt-get install libsqlite3-dev
\n\n

macOS (Homebrew):

\n
brew install sqlite3
\n\n

Fedora:

\n
sudo dnf install sqlite-devel
\n\n

2. Installation on Windows (vcpkg)

\n\n

If you use vcpkg:

\n
vcpkg install sqlite3:x64-windows\nvcpkg integrate install
\n\n

After that, in Visual Studio you can include #include <sqlite3.h>.

\n\n

3. Compiling from source (universal method)

\n\n

Download the sqlite3.c and sqlite3.h files from the official website sqlite.org. Then simply add them to your project:

\n
g++ main.cpp sqlite3.c -o myapp -lpthread -ldl
\n\n

On Windows, you may additionally need to specify -lws2_32.

\n\n

Quick start: first program with SQLite in C++

\n\n

Let's create a simple console application that opens a database, creates a table, and inserts one record.

\n\n
#include <iostream>\n#include <sqlite3.h>\n\nint main() {\n    sqlite3 *db;\n    char *errMsg = 0;\n\n    // 1. Open (or create) the database\n    int rc = sqlite3_open("test.db", &db);\n    if (rc) {\n        std::cerr << "Error opening database: " << sqlite3_errmsg(db) << std::endl;\n        return 1;\n    }\n\n    // 2. Create a table\n    const char *sqlCreate = "CREATE TABLE IF NOT EXISTS users ("\n                            "id INTEGER PRIMARY KEY AUTOINCREMENT, "\n                            "name TEXT NOT NULL, "\n                            "age INTEGER);";\n\n    rc = sqlite3_exec(db, sqlCreate, 0, 0, &errMsg);\n    if (rc != SQLITE_OK) {\n        std::cerr << "SQL error: " << errMsg << std::endl;\n        sqlite3_free(errMsg);\n        sqlite3_close(db);\n        return 1;\n    }\n\n    // 3. Insert data\n    const char *sqlInsert = "INSERT INTO users (name, age) VALUES ('Alice', 30);";\n    rc = sqlite3_exec(db, sqlInsert, 0, 0, &errMsg);\n    if (rc != SQLITE_OK) {\n        std::cerr << "Insert error: " << errMsg << std::endl;\n        sqlite3_free(errMsg);\n    } else {\n        std::cout                    

Recommendations