Embedding the enitre SQLite into C++ program is easier than I think. I have basically followed the demo code of RobertoChapa.
I put everything under the subdirectory db. Then I created a subdirectory sqlite for SQLite. In sqlite, it contains the amalgamated zip file of the source code, the amalgamated source code sqlite3.c
, the header file sqlite3.h
. These are the two main files to interface with the entire SQLite.
This is my directory tree:
db
├── CMakeLists.txt
├── README.md
├── demo.cpp
└── sqlite
├── shell.c
├── sqlite-amalgamation-3440000.zip
├── sqlite3.c
├── sqlite3.h
└── sqlite3ext.h
The content of CMakeLists.txt
:
cmake_minimum_required(VERSION 3.12)
set(include_directories sqlite)
set(MYPROGRAM_FILES sqlite/sqlite3.c)
add_executable(db demo.cpp ${MYPROGRAM_FILES})
add_executable(dbsh sqlite/shell.c ${MYPROGRAM_FILES})
I have added another target called dbsh
for the shell interface. It can be a very handy utility for verifying the content of the demo database. It can be thought as dbu
back in the Clipper Summer ‘87 days.
When I was reading Professional C++ by Marc Gregoire, I input the Employer database program at the end of chapter 1. It is just a program illustrating the characteristic of C++. It cannot store the results, that is, the employee database, on the hard disk. So I have to input from scratch each time I run the program. Adding a secondary solution is the next thing to do. I stopped there as it might be an advanced topic for me. But now it is easier than I think, luckily.
I have forayed into database nevertheless, how about go a bit further? Let’s say can we embed a NoSQL database? After some research here and here, it seems that it is possible. There are quite a lot to choose from. Initially I will take a closer look at either ObjectBox or RocksDB. The main reason is that both of them are in open source C++ projects. So looking into the code will definitely help my C++ learning journey. But before that, I need to have a solid grasp of some other basic C++ concepts. Templates and lambdas are two of them.