Skip to content

Commit a88315b

Browse files
author
Alan Lam
committed
my example
1 parent 92ed68a commit a88315b

File tree

5 files changed

+152
-2
lines changed

5 files changed

+152
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Debug
22
Release
33
build
44
example1
5+
example2
56
*.a
67
.DS_Store
78

CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ set(SQLITECPP_EXAMPLES
149149
examples/example1/main.cpp
150150
)
151151
source_group(example1 FILES ${SQLITECPP_EXAMPLES})
152+
set(SQLITECPP_EXAMPLES2
153+
examples/example2/main.cpp
154+
)
155+
source_group(example2 FILES ${SQLITECPP_EXAMPLES2})
152156

153157
# list of doc files of the library
154158
set(SQLITECPP_DOC
@@ -279,6 +283,24 @@ else (SQLITECPP_BUILD_EXAMPLES)
279283
message(STATUS "SQLITECPP_BUILD_EXAMPLES OFF")
280284
endif (SQLITECPP_BUILD_EXAMPLES)
281285

286+
option(SQLITECPP_BUILD_EXAMPLES2 "Build examples2." OFF)
287+
if (SQLITECPP_BUILD_EXAMPLES2)
288+
# add the basic example executable
289+
add_executable(SQLiteCpp_example2 ${SQLITECPP_EXAMPLES2})
290+
target_link_libraries(SQLiteCpp_example2 SQLiteCpp sqlite3)
291+
# Link target with pthread and dl for linux
292+
if (UNIX)
293+
target_link_libraries(SQLiteCpp_example2 pthread)
294+
if (NOT APPLE)
295+
target_link_libraries(SQLiteCpp_example2 dl)
296+
endif ()
297+
elseif (MSYS OR MINGW)
298+
target_link_libraries(SQLiteCpp_example2 ssp)
299+
endif ()
300+
else (SQLITECPP_BUILD_EXAMPLES2)
301+
message(STATUS "SQLITECPP_BUILD_EXAMPLES2 OFF")
302+
endif (SQLITECPP_BUILD_EXAMPLES2)
303+
282304
option(SQLITECPP_BUILD_TESTS "Build and run tests." OFF)
283305
if (SQLITECPP_BUILD_TESTS)
284306
# deactivate some warnings for compiling the gtest library

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mkdir -p build
1111
cd build
1212

1313
# Generate a Makefile for GCC (or Clang, depanding on CC/CXX envvar)
14-
cmake -DCMAKE_BUILD_TYPE=Debug -DSQLITECPP_USE_GCOV=OFF -DSQLITECPP_BUILD_EXAMPLES=ON -DSQLITECPP_BUILD_TESTS=ON ..
14+
cmake -DCMAKE_BUILD_TYPE=Debug -DSQLITECPP_USE_GCOV=OFF -DSQLITECPP_BUILD_EXAMPLES=ON -DSQLITECPP_BUILD_EXAMPLES2=ON -DSQLITECPP_BUILD_TESTS=ON ..
1515

1616
# Build (ie 'make')
1717
cmake --build .

examples/example1/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,15 @@ int main ()
442442
// Execute the one-step query to insert the blob
443443
int nb = query.exec();
444444
std::cout << "INSERT INTO test VALUES (NULL, ?)\", returned " << nb << std::endl;
445+
query.reset();
446+
SQLite::bind(query, 43, "fortythree");
447+
nb = query.exec();
448+
std::cout << "INSERT INTO test VALUES (NULL, ?)\", returned " << nb << std::endl;
445449
}
446450

447451
SQLite::Statement query(db, "SELECT * FROM test");
448452
std::cout << "SELECT * FROM test :\n";
449-
if (query.executeStep())
453+
while (query.executeStep())
450454
{
451455
std::cout << query.getColumn(0).getInt() << "\t\"" << query.getColumn(1).getText() << "\"\n";
452456
}

examples/example2/main.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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-2019 Sebastien Rombauts (sebastien.rombauts@gmail.com)
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+
#include <string>
17+
#include <fstream>
18+
19+
#include <SQLiteCpp/SQLiteCpp.h>
20+
#include <SQLiteCpp/VariadicBind.h>
21+
22+
23+
#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER
24+
namespace SQLite
25+
{
26+
/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt)
27+
void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg)
28+
{
29+
// Print a message to the standard error output stream, and abort the program.
30+
std::cerr << apFile << ":" << apLine << ":" << " error: assertion failed (" << apExpr << ") in " << apFunc << "() with message \"" << apMsg << "\"\n";
31+
std::abort();
32+
}
33+
}
34+
#endif
35+
36+
/// Get example path
37+
static inline std::string getExamplePath()
38+
{
39+
std::string filePath(__FILE__);
40+
return filePath.substr( 0, filePath.length() - std::string("main.cpp").length());
41+
}
42+
43+
/// Example Database
44+
static const std::string filename_example_db3 = getExamplePath() + "/example.db3";
45+
/// Image
46+
static const std::string filename_logo_png = getExamplePath() + "/logo.png";
47+
48+
49+
/// Object Oriented Basic example
50+
class Example
51+
{
52+
public:
53+
// Constructor
54+
Example() :
55+
mDb(filename_example_db3), // Open a database file in readonly mode
56+
mQuery(mDb, "SELECT * FROM test WHERE weight > :min_weight")// Compile a SQL query, containing one parameter (index 1)
57+
{
58+
}
59+
virtual ~Example()
60+
{
61+
}
62+
63+
/// List the rows where the "weight" column is greater than the provided aParamValue
64+
void ListGreaterThan (const int aParamValue)
65+
{
66+
std::cout << "ListGreaterThan (" << aParamValue << ")\n";
67+
68+
// Bind the integer value provided to the first parameter of the SQL query
69+
mQuery.bind(":min_weight", aParamValue); // same as mQuery.bind(1, aParamValue);
70+
71+
// Loop to execute the query step by step, to get one a row of results at a time
72+
while (mQuery.executeStep())
73+
{
74+
std::cout << "row (" << mQuery.getColumn(0) << ", \"" << mQuery.getColumn(1) << "\", " << mQuery.getColumn(2) << ")\n";
75+
}
76+
77+
// Reset the query to be able to use it again later
78+
mQuery.reset();
79+
}
80+
81+
private:
82+
SQLite::Database mDb; ///< Database connection
83+
SQLite::Statement mQuery; ///< Database prepared SQL query
84+
};
85+
86+
int main ()
87+
{
88+
// Using SQLITE_VERSION would require #include <sqlite3.h> which we want to avoid: use SQLite::VERSION if possible.
89+
// std::cout << "SQlite3 version " << SQLITE_VERSION << std::endl;
90+
std::cout << "SQlite3 version " << SQLite::VERSION << " (" << SQLite::getLibVersion() << ")" << std::endl;
91+
std::cout << "SQliteC++ version " << SQLITECPP_VERSION << std::endl;
92+
93+
std::ifstream infile("/home/alan/Downloads/tmp/en-zh/UNv1.0.en-zh.en");
94+
std::string line;
95+
////////////////////////////////////////////////////////////////////////////
96+
// Simple batch queries example (5/7) :
97+
try
98+
{
99+
// Open a database file in create/write mode
100+
SQLite::Database db("test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
101+
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
102+
103+
// Create a new table with an explicit "id" column aliasing the underlying rowid
104+
db.exec("DROP TABLE IF EXISTS en");
105+
db.exec("CREATE TABLE en (sentence TEXT)");
106+
SQLite::Statement query(db, "INSERT INTO en VALUES(?)");
107+
108+
while (std::getline(infile,line)) {
109+
SQLite::bind(query,line.c_str());
110+
query.exec();
111+
query.reset();
112+
}
113+
}
114+
catch (std::exception& e)
115+
{
116+
std::cout << "SQLite exception: " << e.what() << std::endl;
117+
return EXIT_FAILURE; // unexpected error : exit the example program
118+
}
119+
120+
std::cout << "everything ok, quitting\n";
121+
122+
return EXIT_SUCCESS;
123+
}

0 commit comments

Comments
 (0)