Skip to content

Commit 062ad5b

Browse files
author
Sébastien Rombauts
committed
Add a simplified main.cpp from SQLiteCpp "example1", an appropriate CMake config file and build.sh/.bat scripts
1 parent 46fc505 commit 062ad5b

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Debug
2+
Release
3+
build
4+
example1
5+
*.a
6+
7+
/SQLiteCpp.sln
8+
*.ncb
9+
*.suo
10+
*.user
11+
*sdf
12+
*.vc*
13+
*~
14+
doc
15+
core
16+
*ipch
17+
.settings/
18+
19+
CMakeCache.txt
20+
CMakeFiles
21+
*.cmake
22+
*.dir
23+
Testing
24+
Win32
25+
26+
SQLiteCpp_example1
27+
SQLiteCpp_tests
28+
29+
!FindSQLiteCpp.cmake
30+
/SQLiteCpp_Example

CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 2.8.12) # first version with add_compile_options()
2+
project(SQLiteCpp_Example)
3+
4+
# add SQLite3 C++ wrapper arround sqlite3 library (and sqlite3 itself provided for ease of use)
5+
include_directories(SQLiteCpp/include)
6+
add_subdirectory(SQLiteCpp)
7+
8+
# add main.cpp example source code
9+
set(SOURCE_FILES src/main.cpp)
10+
add_executable(SQLiteCpp_Example ${SOURCE_FILES})
11+
12+
# Link SQLiteCpp_example1 with SQLiteCpp and sqlite3 (and also pthread and dl for linux)
13+
if(UNIX AND NOT APPLE)
14+
# Linux
15+
target_link_libraries(SQLiteCpp_Example SQLiteCpp sqlite3 pthread dl)
16+
else(UNIX AND APPLE)
17+
# Mac OS
18+
target_link_libraries(SQLiteCpp_Example SQLiteCpp sqlite3 pthread)
19+
else()
20+
# Windows
21+
target_link_libraries(SQLiteCpp_Example SQLiteCpp sqlite3)
22+
endif()

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-2016 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+
cd ..
17+
exit
18+
19+
:onError
20+
@echo An error occured!
21+
cd ..

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-2016 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 firts 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+

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-2016 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)