What is SQLite and why is it needed in C++?
\n\nSQLite 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\nFor 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\nKey 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
Installing SQLite for C++
\n\nInstallation depends on your operating system and compiler. We will cover three main methods.
\n\n1. Installation via package manager (Linux/macOS)
\n\nUbuntu/Debian:
\nsudo apt-get update\nsudo apt-get install libsqlite3-dev\n\nmacOS (Homebrew):
\nbrew install sqlite3\n\nFedora:
\nsudo dnf install sqlite-devel\n\n2. Installation on Windows (vcpkg)
\n\nIf you use vcpkg:
\nvcpkg install sqlite3:x64-windows\nvcpkg integrate install\n\nAfter that, in Visual Studio you can include #include <sqlite3.h>.
3. Compiling from source (universal method)
\n\nDownload the sqlite3.c and sqlite3.h files from the official website sqlite.org. Then simply add them to your project:
g++ main.cpp sqlite3.c -o myapp -lpthread -ldl\n\nOn Windows, you may additionally need to specify -lws2_32.
Quick start: first program with SQLite in C++
\n\nLet'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