SQLite PLCnext C++ project, using the database

Hello,
I have successfully followed the tutorial on how to use SQLite in PLCnext C++ program (https://www.plcnext-community.net/makersblog/how-to-use-sqlite-in-a-plcnext-c-project/).
How do i expand on this to actually write/read data from the PLC? For example, where should i write code to add entries to SQLite table in Eclipse and how should i trigger the adding event from PLCnext?

Thank you.

Hello Codeka,
if you are working on PLCnext application, the siplest and most efficient solution is to use the integrated DataLogger FW-Service. The DL-Service uses the SQLite implementation and you can easily set the trigger condition in PLCnext Engineer “Datalogger-Configuration”-Sheet: DataLogger concept (plcnext.help).
Generally you have the following options:

                                          1. You can use the DataLogger Configuration-Sheet in PLCnext Engineer IDE to log the GDS-Variables (in SQLite-DB). The GDS-Variables can be declared/updated in IEC-Program, C++ Program or Matlab-Simulink-Program.
                                          2. Or if you are familiar with C++ Programming and the special application is needed, you can use the DataLogger- API ([PLCnext API Documentation: Main Page](https://api.plcnext.help/api_docs_2022-0-LTS/index.html)) and implement your own C++ Application, the following Example can help: [PLCnext/PLCnext_RT_Datalogger: The dataLogger is a service component of the PLCnext Technology firmware that provides real time data logging for Global Data Space ports. (github.com)](https://github.com/PLCnext/PLCnext_RT_Datalogger)

In order to implement an application for safe use in the field, based on example in makersblog, further implementation steps and tests are necessary. To avoid this effort for the customers, the development of the DL service was realized.
Please send me a short feedback if my suggestion is useful for you.
Thanks & BR
Eduard

There is also a C++ example from another Community member that builds on the Makers Blog post you mentioned:
How to store data in SQLite in a PLCnext C++ project - PLCnext Community This article describes how the SQLite database engine allready installed on PLCnext Controllers could be used to store data provided via the Global Data Space (GDS). The database enables the storage of process data in a standardized way and could be exportet to other systems with SFTP. Prerequisites Create a new project or modify an […] I think that article does at least some of the things you want.
I agree with Eduard, the Data Logger service should also be considered.

Thank you for the quick reply Eduard and Martin!

I tried the DataLogger by adding a new DataLogger session with test variable. Test variable adds 1 with every cycle and resets to 0 when its greater than 1000. Type is “Database” (i dont have a SD card at the moment). I can see database file with WinSCP and when i open it with DB browser i can see that it is logging the values correctly.
I dont see how i could read the value to the PLC? Later i would like to use DataLogger Trigger function to write data to database and then use another trigger to read data from the database. I will also need to make querys for what data i want to read. For example, find all data with certain id and return the product that was added first.
Data structure that i need to store in the database:
ST_Product : STRUCT
Id : STRING;
Weight : PBCL_QualifiedFloat32;
Dimensions : ST_Dimensions;
TimeAdded : RTC_TYPE;
Shelf : INT;
Module : INT;
END_STRUCT
ST_Dimensions : STRUCT
Height : REAL;
Depth : REAL;
Width : REAL;
END_STRUCT
So i would be storing a variable with type “ST_Product” in to the database.

I also tried “How to store data in SQLite in a PLCnext C++ project” and got some errors in the Component.hpp file: “Type ‘WorkerThread’ could not be resolved” and various errors when modifying inline methods (Member declaration not found and symbols could not be resolved).
StorageDBComponent.hpp.txt

StorageDBComponent.hpp.txt

That Makers Blog post was written a good few years ago now, so I suspect the problem is that later versions of the PLCnext CLI code generator/parser and/or the SDK are different enough that the code in that example no longer compiles. You could post a comment under that blog, asking the author to check this and (if necessary) update the blog post.
There is currently no simple mechanism (that I know of) for getting data from a sqlite db into PLCnext Engineer. Data from the DataLogger db can be retrieved using an OPC UA client that supports Historical Data Access, but I don’t know of any HDA client like this for PLCnext Engineer. The closest is the DBFL_SQL library for PLCnext Engineer, but that doesn’t currently support sqlite dbs.
The only option I can think of is to write a custom C++ component that retrieves data from the sqlite database files generated by the data logger, and passes this data (somehow) to PLCnext Engineer. We don’t publish the schema for the DataLogger db, but you might be able to figure that out. And you will need to be careful not to interfere with db writes (or any other Data Logger operation), while you are reading from the db file(s).
I hope this helps.

I could use any database that is suitable for read/write and making querys like find certain “ID” and retrieve variable that was added first (TimeAdded). So it doesent have to be a SQLite database if there is a better way.

I have also heard that one option could be a Docker container database running on Linux side of the PLC. Im not familiar with Docker but i understand the basic idea of creating a docker runtime on Linux side and then run database image on it. I dont know how would i actually use that database from PLC (read/write/querys). Maybe i could use DBFL_SQL library?

Hello Codeka,
It is possible to run the DB in the Docker container, but I understand that you need access to logged data in the PLCnext Engineer application. The implementation of queries is also required to read data in the SQLite database running in docker.
I would like to recommend you following options:
You can use the DataLogger Library in AppStore to log the Data in .csv format. You will find also the complete example for logging and readning data (PLCnext Store | DataLogger
1. ).
2. As described, you can use the IDataLogger Service provided by FW and implement your own C++ Application for reading data from SQLite DB. I Implemented a step by step the example, you can find it on GitHub: PLCnext/PLCnext_RT_Datalogger: The dataLogger is a service component of the PLCnext Technology firmware that provides real time data logging for Global Data Space ports. (github.com)
3. Currently the PLCnext Engineer Library will be implemented for IDataLogger Service with the needed functionality in your project. If you can wait, may be it will be a possible way.
BR Eduard

Now the system is able to read and write (atleast simple variables) from PLCnext to database. What worked for me was to install BalenaEngine runtime on PLC’s linux side and then to run MariaDB image container on balena. Then i was able to use DBFL_SQL library that comes with complete example for mariadb.
Thank you Eduard and Martin for the support.