Skip to content

Commit adad71b

Browse files
committed
Add an example2 based on SQLiteCpp_Example repository
Demonstrates how to use SQLiteCpp as a subdirectory (out of tree) of a CMake project.
1 parent 801ed91 commit adad71b

File tree

8 files changed

+203
-3
lines changed

8 files changed

+203
-3
lines changed

.github/workflows/actions.yml renamed to .github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
}
3939

4040
steps:
41-
- uses: actions/checkout@v1
41+
- uses: actions/checkout@v2
4242
- name: submodule
4343
run: git submodule update --init --recursive
4444
- name: configure

.github/workflows/subdir_example.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: subdir_example
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
name: ${{ matrix.config.name }}
8+
runs-on: ${{ matrix.config.os }}
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
config:
13+
- {
14+
name: "Windows Latest MSVC",
15+
os: windows-latest,
16+
build_type: "Debug", cc: "cl", cxx: "cl",
17+
}
18+
- {
19+
name: "Ubuntu Latest GCC",
20+
os: ubuntu-latest,
21+
build_type: "Debug", cc: "gcc", cxx: "g++"
22+
}
23+
- {
24+
name: "Ubuntu 16.04 GCC",
25+
os: ubuntu-16.04,
26+
build_type: "Debug", cc: "gcc", cxx: "g++"
27+
}
28+
- {
29+
name: "macOS Latest Clang",
30+
os: macos-latest,
31+
build_type: "Debug", cc: "clang", cxx: "clang++"
32+
}
33+
34+
steps:
35+
- uses: actions/checkout@v2
36+
- name: configure
37+
shell: cmake -P {0}
38+
run: |
39+
set(ENV{CC} ${{matrix.config.cc}})
40+
set(ENV{CXX} ${{matrix.config.cxx}})
41+
- name: generate
42+
run: |
43+
cd examples/example2
44+
mkdir build
45+
cd build
46+
cmake -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=${{matrix.config.build_type}} ..
47+
- name: build
48+
run: cmake --build examples/example2/build --config ${{matrix.config.build_type}}

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# or copy at http://opensource.org/licenses/MIT)
77
cmake_minimum_required(VERSION 3.1) # for "CMAKE_CXX_STANDARD" version
88
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") # custom CMake modules like FindSQLiteCpp
9-
project(SQLiteCpp VERSION "2.99")
9+
project(SQLiteCpp VERSION 2.99)
1010

1111
# SQLiteC++ 3.x now requires C++11 compiler
1212
set(CMAKE_CXX_STANDARD 11)
@@ -145,7 +145,8 @@ source_group(doc FILES ${SQLITECPP_DOC})
145145
set(SQLITECPP_SCRIPT
146146
.editorconfig
147147
.gitbugtraq
148-
.github/workflows/actions.yml
148+
.github/workflows/build.yml
149+
.github/workflows/subdir_example.yml
149150
.gitignore
150151
.gitmodules
151152
.travis.yml

examples/example2/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Example CMake file for compiling & linking a project with the the SQLiteCpp wrapper
2+
#
3+
# Copyright (c) 2012-2020 Sebastien Rombauts ([email protected])
4+
#
5+
# Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
6+
# or copy at http://opensource.org/licenses/MIT)
7+
cmake_minimum_required(VERSION 3.1) # for "CMAKE_CXX_STANDARD" version
8+
project(SQLiteCpp_Example VERSION 2.0)
9+
10+
# SQLiteC++ 3.x now requires C++11 compiler
11+
set(CMAKE_CXX_STANDARD 11)
12+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
13+
14+
# Add SQLite3 C++ wrapper around sqlite3 library (and sqlite3 itself provided for ease of use)
15+
# Here you can set CMake variables to avoid building Example, as well as cpplint, cppcheck...
16+
# or set them in the cmake command line (see for instance provided build.bat/build.sh scripts)
17+
set(SQLITECPP_RUN_CPPCHECK OFF CACHE BOOL "" FORCE)
18+
set(SQLITECPP_RUN_CPPLINT OFF CACHE BOOL "" FORCE)
19+
add_subdirectory(../.. SQLiteCpp) # out-of-source build requires explicit subdir name for compilation artifacts
20+
21+
# Add main.cpp example source code to the executable
22+
add_executable(SQLiteCpp_Example src/main.cpp)
23+
24+
# Link SQLiteCpp_example1 with SQLiteCpp
25+
target_link_libraries(SQLiteCpp_Example SQLiteCpp)

examples/example2/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SQLiteCpp_Example demonstrates how to use SQLiteCpp as a subdirectory of a CMake project.
2+
3+
See https://github.com/SRombauts/SQLiteCpp_Example

examples/example2/build.bat

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@REM Copyright (c) 2012-2020 Sebastien Rombauts ([email protected])
2+
@REM
3+
@REM Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
4+
@REM or copy at http://opensource.org/licenses/MIT)
5+
mkdir build
6+
cd build
7+
8+
@REM Generate a Visual Studio solution for latest version found
9+
cmake ..
10+
@if ERRORLEVEL 1 goto onError
11+
12+
@REM Build default configuration (ie 'Debug')
13+
cmake --build .
14+
@if ERRORLEVEL 1 goto onError
15+
16+
goto onSuccess
17+
18+
:onError
19+
@echo An error occured!
20+
:onSuccess
21+
cd ..

examples/example2/build.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
# Copyright (c) 2012-2020 Sébastien Rombauts ([email protected])
3+
#
4+
# Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
5+
# or copy at http://opensource.org/licenses/MIT)
6+
7+
# exit on first error
8+
set -e
9+
10+
mkdir -p build
11+
cd build
12+
13+
# Generate a Makefile for GCC (or Clang, depanding on CC/CXX envvar)
14+
cmake -DCMAKE_BUILD_TYPE=Debug ..
15+
16+
# Build (ie 'make')
17+
cmake --build .
18+

examples/example2/src/main.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @file main.cpp
3+
* @brief A few short examples in a row.
4+
*
5+
* Demonstrates how-to use the SQLite++ wrapper
6+
*
7+
* Copyright (c) 2012-2020 Sebastien Rombauts ([email protected])
8+
*
9+
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
10+
* or copy at http://opensource.org/licenses/MIT)
11+
*/
12+
13+
#include <iostream>
14+
#include <cstdio>
15+
#include <cstdlib>
16+
17+
#include <SQLiteCpp/SQLiteCpp.h>
18+
19+
20+
#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER
21+
namespace SQLite
22+
{
23+
/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt)
24+
void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg)
25+
{
26+
// Print a message to the standard error output stream, and abort the program.
27+
std::cerr << apFile << ":" << apLine << ":" << " error: assertion failed (" << apExpr << ") in " << apFunc << "() with message \"" << apMsg << "\"\n";
28+
std::abort();
29+
}
30+
}
31+
#endif
32+
33+
int main ()
34+
{
35+
// Using SQLITE_VERSION would require #include <sqlite3.h> which we want to avoid: use SQLite::VERSION if possible.
36+
// std::cout << "SQlite3 version " << SQLITE_VERSION << std::endl;
37+
std::cout << "SQlite3 version " << SQLite::VERSION << " (" << SQLite::getLibVersion() << ")" << std::endl;
38+
std::cout << "SQliteC++ version " << SQLITECPP_VERSION << std::endl;
39+
40+
////////////////////////////////////////////////////////////////////////////
41+
// Simple batch queries example :
42+
try
43+
{
44+
// Open a database file in create/write mode
45+
SQLite::Database db("test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
46+
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
47+
48+
// Create a new table with an explicit "id" column aliasing the underlying rowid
49+
db.exec("DROP TABLE IF EXISTS test");
50+
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
51+
52+
// first row
53+
int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")");
54+
std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl;
55+
56+
// second row
57+
nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")");
58+
std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl;
59+
60+
// update the second row
61+
nb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'");
62+
std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl;
63+
64+
// Check the results : expect two row of result
65+
SQLite::Statement query(db, "SELECT * FROM test");
66+
std::cout << "SELECT * FROM test :\n";
67+
while (query.executeStep())
68+
{
69+
std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\")\n";
70+
}
71+
72+
db.exec("DROP TABLE test");
73+
}
74+
catch (std::exception& e)
75+
{
76+
std::cout << "SQLite exception: " << e.what() << std::endl;
77+
return EXIT_FAILURE; // unexpected error : exit the example program
78+
}
79+
remove("test.db3");
80+
81+
std::cout << "everything ok, quitting\n";
82+
83+
return EXIT_SUCCESS;
84+
}

0 commit comments

Comments
 (0)