C++

SQLite – How to use fundamental functions in C++

SQLite operations in C++

This tutorial aimed to demonstrate how to use SQLite database operations such as CREATE, INSERT, UPDATE, SELECT and DELETE in C++. Since the appearance first smartphone and other mobile devices SQLite become most widely deployed and used database engine in the world. Precise numbers are difficult to obtain but SQLite is likely used more than all other database engines combined. We can find SQLite in every Android device, iPhone and iOS device, Mac, Windows10, Firefox, Chrome, Safari, Skype, Dropbox, TurboTax, QuickBooks, PHP, Python, television sets, set-top cable boxes and many automotive multimedia systems.

It’s an open-source SQL database engine. You can download the source code or precompiled binaries depending your requirements. You might also need to add [-lsqlite3] and [-std=c++17] to successfully compile, create an executable file. Each of the following source code available on my git repository.

github.com/lnrsoft/sqlite3_cpp_basics_updated
or
lnrrepo.ddns.net/git/lnrsoft/sqlite3_cpp_basics_updated

Use this link local_db.sqlite (6 downloads) if you would like to download the local_db.sqlite file that I used in this tutorial.

Step 1 – The first thing we need to do is to create the actual database file that we will use in this article. I called sqltdemo.db in our case.

Compile and run our source code to create the local_db.sqlite with the following command. Here we link our code with the sqlite3 library that will create our db file.

Verify our result:

Step 2 – CREATE TABLE

Compile and run.

Step 3 – INSERT INTO our CANDIDATE TABLE

Compile and run.

Step 4 – Fetch and display records from the company CANDIDATE TABLE. In this scenario we use a callback function that obtains results from SELECT statement.

Compile and run.

Step 5 – Using UPDATE statement to update any records in our CANDIDATE table.

Compile and run.

Step 6 – DELETE statement can delete any records from the CANDIDATE table.

Compile and run.

I recommend you to check the official SQLite documentation at sqlite.org for more advance SQLite features and options and support. For further info about using sql with c++ you might found it useful to read me previous post of  a simple Qt tool to test QSqlDatabase access connection to a MySQL database. I created this simple Qt command line tool to test connection between Qt client applications and a remote Mysql Server. The code includes Secure Sockets Layer (SSL) support to test secure connection. The QSqlDatabase class provides an interface for accessing a database through a connection where an instance of QSqlDatabase represents the connection. The connection provides access to the database via one of the supported database drivers, which are derived from QSqlDriver.

Alternatively, we can subclass your own database driver from QSqlDriver. My next post will be an article how to write your own database driver in Qt for Unix, OSX and Windows platforms.

[C++] MemorySanitizer

MemorySanitizer is a detector of uninitialized memory reads in C/C++ programs. Uninitialized values occur when stack- or heap-allocated memory is read before it is written. MSan detects cases where such values affect program execution.
At this time, MemorySanitizer supports Linux x86_64 only.
To use MemorySanitizer, compile and link your program with -fsanitize=memory -fPIE -pie. To get any stack traces, add -fno-omit-frame-pointer.

Origins tracking

[C++] AddressSanitizer

AddressSanitizer is a memory error detector for C/C++. It finds:

-Use after free (dangling pointer dereference)
-Heap buffer overflow
-Stack buffer overflow
-Global buffer overflow
-Use after return
-Use after scope
-Initialization order bugs
-Memory leaks

In order to use AddressSanitizer we will need to compile and link your program using clang with the -fsanitize=address switch. To get a reasonable performance add -O1 or higher.

[C++] ThreadSanitizer

ThreadSanitizer is a data race detector for C/C++. Data races are one of the most common and hardest to debug types of bugs in concurrent systems.
Here is an example of a data race that can lead to crashes and memory corruptions:

[C++] How to install gcc-7 on Ubuntu LTS 16.04

[C++] Return absolute_path using boost::filesystem

Return absolute_path using boost::filesystem

Note:
You’ll need to add the boost libraries to the linking phase with the -L flag and the names of the libraries with the -l flag. This is different from the compilation phase where only the -I include path is needed. For example in my case:

boost::filesystem tutorials
https://meetingcpp.com/blog/items/handling-files-using-boost-filesystem.html
http://www.boost.org/doc/libs/1_55_0/libs/filesystem/doc/tutorial.html
http://www.boost.org/doc/libs/1_44_0/libs/filesystem/v3/doc/tutorial.html
http://www.boost.org/doc/libs/1_51_0/libs/filesystem/doc/reference.html#Absolute-path
http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/reference.html
http://www.boost.org/doc/libs/1_55_0/libs/filesystem/doc/reference.html#copy_file
http://wiki.lihebi.com/boost.html

The Boost C++ Libraries
AdvC++Libs.key
boost-graph-library
asio_doc
dima-boost-intro
Boost_for_KDE_Developers-Volker_Krause

[C++] An Interesting Bit Array Example

Here is an interesting bit array task and its solution in C++ that I thought it’s worth to share with you.
You are given four integers values: N, A, B, and C. You need to use them in order to create the sequence called bitArray with the following pseudo-code:

Task: calculate the number of distinct integer in the bitArray sequence.

Solution
Input: four space separated integers on a single line, N, A, B, and C  respectively.
Where N = 32, A = 16, B = 16, C = 16.
Output: 
one integer that denotes the number of distinct integer in the bitArray sequence equals 7 as we set N, A, B, C values.

If you’d like to read the input from the console simply comment out line 57 and 62 and toggle comment selection on line 58, 59, 60 and 61.
Input read from the console:

four space separated integers on a single line, N, A, B, and C  respectively.
Output print to the console: 
One integer that denotes the number of distinct integer in the sequence called bitArray.

The number of distinct integer  in the sequence  will be 536870912 when we change the previously set integer values to:

Use Wandbox online C++ compiler

[C++] cxx_status and support in Clang

Here is the list of current C++ implementation status and support in Clang as per Aug 2017.
You can find the complete list of features and minimum Clang version with support on the source page: https://clang.llvm.org/cxx_status.html

C++11 implementation status
Clang 3.3 and later implement all of the ISO C++ 2011 standard. By default, Clang builds C++ code according to the C++98 standard, with many C++11 features accepted as extensions. You can use Clang in C++11 mode with the -std=c++11 option. Clang’s C++11 mode can be used with libc++ or with gcc’s libstdc++.

C++14 implementation status
Clang 3.4 and later implement all of the ISO C++ 2014 standard.

You can use Clang in C++14 mode with the -std=c++14 option (use -std=c++1y in Clang 3.4 and earlier).

C++17 implementation status
Clang 5 and later implement all the features of the C++ 2017 Draft International Standard.
You can use Clang in C++17 mode with the -std=c++17 option (use -std=c++1z in Clang 4 and earlier).

C++2a implementation status
Clang has experimental support for some proposed features of the C++ standard following C++17, provisionally named C++2a. Note that support for these features may change or be removed without notice, as the draft C++2a standard evolves.
You can use Clang in C++2a mode with the -std=c++2a option.

More technical specifications and standing documents on the source page: https://clang.llvm.org/cxx_status.html

CMAKE BUILD TYPES AND HOW TO SPECIFY COMPILERS AND COMPILER FLAGS

CMake has a CMAKE_BUILD_TYPE option for predefined build types. The build type affects optimization and whether the result of the build is debuggable. Those used by MySQL are RelWithDebInfo or Debug.
RelWithDebInfo (optimizations are on, debug info is generated) is used in MySQL by default.
Debug (optimizations are off, debug info is generated) is used if the WITH_DEBUG option is set.

CMAKE_BUILD_TYPE is not set when custom compile flags are used

To define which C and C++ compilers to use, you can define the CC and CXX environment variables. For example:

To specify your own C and C++ compiler flags, for flags that do not affect optimization, use the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS CMake options:

When providing your own compiler flags, you might want to specify CMAKE_BUILD_TYPE as well.
For example, to create a 32-bit release build on a 64-bit Linux machine, do this:

If you set flags that affect optimization (-Onumber), you must set the CMAKE_C_FLAGS_build_type and/or CMAKE_CXX_FLAGS_build_type options, where build_type  corresponds to the CMAKE_BUILD_TYPE value.

To specify a different optimization for the default build type (RelWithDebInfo) set the CMAKE_C_FLAGS_RELWITHDEBINFO and CMAKE_CXX_FLAGS_RELWITHDEBINFO options.
For example, to compile on Linux with -O3 and with debug symbols, do this: