Working With Databases in C++ An Introduction by Dane Bulat Medium
Working With Databases in C++ An Introduction by Dane Bulat Medium
dane-bulat.medium.com
36–46 minutos
Introduction
1 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
What is SOCI?
2 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
Prerequisites
• C++ Compiler
Such as GCC, LLVM/Clang or Microsoft Visual C++.
• CMake 2.8+
Used for building the SOCI API and libraries. Download CMake or
install the cmake package using your OS’s native package
manager.
3 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
For readers who are using Windows, please refer to the boost
download page where you can download and install Boost via
prebuilt Windows binaries. Also refer to the SOCI Installation page
for specific instructions on running CMake on Windows and
installing SOCI to your system.
Installing Boost
4 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
Installing SOCI
That might sound like a lot of steps to break down and understand,
so let’s start by executing the following two commands in a
5 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
cd ~
git clone git://github.com/SOCI/soci.git
6 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
• make
A program that will read the Makefile in the build directory and
generate the shared libraries, source files, and binaries that make
up the SOCI library.
• make install
This command will copy the generated shared libraries and include
files to appropriate locations on your filesystem.
7 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
the end of this article, we will configure the C++ compiler to search
through these directories in order to resolve our application’s
header and library dependencies.
8 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
runtime.
If you are already familiar with the SQL statements used in the
script and do not feel like further explanation is necessary, feel free
to download the file and execute the script from your MySQL
server.
9 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
Once logged into MySQL as the root user, run MySQL’s source
command to execute the script:
source /path/to/script.sql
If you have executed the script and understand all the SQL
commands, feel free to proceed to the next section in this article
(Developing a SOCI C++ Application).
We will firstly log in to our MySQL server as the root user. Let’s
open up a terminal and run the following command:
mysql -u root -p
Enter password: <your root password>
Let’s proceed to create a new database and use it via the following
two statements:
10 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
USE soci_db;
• The id column serves as the primary key, which means that all
records in the users table can be uniquely identified with the id
value. The id column of the first row inserted into the table will
contain one. The AUTO_INCREMENT property will also increment id
by one for each new record.
11 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
To verify that our table has been created successfully, lets describe
it with the DESC statement:
DESC users;
It’s good practice to study the output of the DESC statement and
confirm that the table has the correct column names, data types,
and constraints set up as intend before moving forward.
Creating a Role
12 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
13 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
We can verify that the ALL privilege has been granted to our role
by executing the SHOW GRANTS statement:
14 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
Creating a User
Let’s go ahead and create a new user on the MySQL server called
soci_dev1. This user will log in to the MySQL server from our C++
application. The CREATE USER statement creates a new user:
Let’s confirm that the role is applied to the user by executing the
SHOW GRANTS statement:
15 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
role_soci_dev;
Now, every role that has been granted to our user will be active, by
default, when they log in to MySQL.
16 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
SELECT user();
17 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
GRANTS statement:
SHOW GRANTS;
Notice that we have used the short form of the SHOW GRANTS
statement to display our own privileges.
SHOW DATABASES;
Lastly, lets enter the soci_db database and show the users table
by running the following statements:
USE soci_db;
SHOW TABLES;
18 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
tasks:
• Insert a new user into the users table. The application will display
prompts allowing you to input values for the new user’s first name,
last name, and email address.
• Retrieve and display all records from the users table. For every
row that is retrieved from the database, the value inside each
column will be output to the terminal.
The following gist contains the entire C++source code for this
demonstration. Feel free to type out the code in your own
hello.cpp source file while trying to understand how the
application works.
Alternatively, you can download the source file and proceed to the
Compiling and Running Our Application section to run the
program. After running the program, you may wish to return to this
section to type out the code yourself and study the explanations
that follow:
19 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
#include <soci/soci.h>
#include <soci/mysql/soci-mysql.h>
#include <exception>
#include <iostream>
#include <string>
20 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
Exception Handling
21 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
22 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
Getting Data
Let’s take a look at inserting a new user record into the users
table using SOCI. Furthermore, it would be nice if we could
manually type in values for the new user’s first name, last name,
and email columns.
template<typename T>
void get_data(const string prompt, T& value) {
cout << prompt;
cin >> value;
}
23 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
• template<typename T>
The function get_data is in fact a template function. In the first
line we declare a type called T, and use it as a placeholder for the
function’s second parameter.
Inserting Data
24 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
25 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
26 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
cout << "> Successfully inserted user." << endl << endl;
Retrieving Data
27 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
users table
rowset<row> rs = (sql.prepare << "SELECT * FROM users");
// Iterate through the result set
for (rowset<row>::const_iterator it = rs.begin();
it != rs.end(); ++it) { const row& r = *it;
std::cout << "ID: " << r.get<int>(0) << endl
<< "First Name: " << r.get<string>(1) << endl
<< "Last Name: " << r.get<string>(2) << endl
<< "Email: " << r.get<string>(3) << endl
<< "Active: " << r.get<int>(4) << endl << endl;
}
}
Let’s firstly take a look at how we can retrieve row data from
MySQL:
With the rows fetched from the database backend and returned to
28 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
our application, we then enter a for loop and iterate through the
result set:
29 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
row.get<column_data_type>(column_index)
Let’s move forward and talk about compiling and running our
application!
This section will walk through the process of compiling and running
our application. The instructions presented in this section will
primarily focus on UNIX environments — such as the Mac OS X
and Linux operating systems.
For readers who are using Windows, feel free to read through the
30 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
following instructions and try to mirror the process using your IDE’s
compiler settings. As soon as I’m in possession of a Windows
system, this section will be updated with specific instructions for
Windows OS!
To check if your system has the g++ compiler installed, run the
following command inside a terminal:
which g++
The path to the g++ binary should output to the terminal after
running this command — a typical location is /usr/bin/g++. If
nothing is returned, either:
export CPATH="/usr/local/include:/usr/local/mysql/include"
31 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
export LIBRARY_PATH="/usr/local/lib:/usr/local/mysql/lib"
export LD_LIBRARY_PATH="/usr/local/lib:/usr/local/mysql/lib"
With the search paths set up we are now ready to compile the
application using the g++ command. After navigating to the
directory containing your hello.cpp source file, execute the
following command:
The following list breaks down each argument we provide the g++
command:
32 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
./hello
In Summary
33 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
I have also be published an article that builds directly from this one
and walks through developing a more sophisticated application
with SOCI and MySQL:
Keep in mind that you will need to include the correct SOCI header
34 of 35 20/08/2023, 21:10
Working with Databases in C++: An Introduction | by Dane Bulat | Medium about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdane-bulat.medium.com%2Fwork...
35 of 35 20/08/2023, 21:10