• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
CDP Studio logo

CDP Studio

The no-code and full-code software development tool for distributed control systems and HMI

  • Doc
  • Why CDP
    • Software developers
    • Automation engineers
    • Managers
  • Products
    • Automation Designer
    • HMI Designer
    • Maritime HMIs
  • Services
  • Use cases
  • Pricing
  • Try CDP

CDP Studio Documentation

  • Examples and Tutorials
  • Using SQL Database in C++ Object

UI for industrial IO HW video tutorials Adding User Authentication to Automation System

Using SQL Database in C++ Object

Introduction

The purpose of this example is to demonstrate how to use the CDP2SQL library to connect to a SQL database with CDP Studio, the independent automation software for open PC-based real-time distributed control systems.

In this example, a database is used to store the message history of a chat application. The chat GUI is made using web technologies (HTML5, css and javascript).

For more information about how to use web user interfaces with CDP Studio, see the GUI - Web UI documentation. To learn more about connecting to SQL databases, have a look at the Framework - CDP2SQL manual.

How to Run the Example

To run the example from CDP Studio, open Welcome mode and find it under Examples. Next, in Configure mode right-click on the library project and select Build, then right-click on the system project and select Run & Connect. See the Running the Example Project tutorial for more information.

When the application is running and connected, click on the application in Configure mode Project tree and choose the Table Editor tab. Go to the Properties section, find the property called WebURL. This is the link for the web browser. You can also open the browser directly by clicking the icon next to the url. Note that there is a WebFilesToServe property in the application configuration that must list all files and folders that should be accessible in the web browser. If the WebFilesToServe is not set up correctly, then the web browser might not show the content correctly, or it might not function correctly. Then the web-page displayed above should open in your web browser.

Structure of the Project

This project has two parts:

  • ChatLib - a library project containing the C++ code for connecting to a SQL database.
  • Chat - a system project containing the ChatApp which includes the HTML5 Web UI.

The focus of this example is on the C++ part. To see the implementation, go to Code mode and open the ChatComponent.cpp under ChatLib/Sources.

ChatComponent

The ChatComponent is based on the Threaded Component Model wizard. As database I/O can be slow, it must always run in a background thread. Otherwise it may interfere with CDP real-time code.

The component has two properties: MessageHistory and MessageToSend. When the latter is modified, the following happens:

  • A property validate handler called WriteLine() is triggered. This buffers a new chat line and triggers Main thread (database thread).
  • In Main(), first a connection to the database is opened. Then the buffered chat line is inserted to the database. Finally, chat history is published into the MessageHistory property.

The example by default uses SQLite but commented out code is provided for MySQL/MariaDB as well in the Main() method:

void ChatComponent::Main()
{
    m_db.ConnectTo(SQLite3Factory().Create(), "database.db");
//    For MySQL or Mariadb use following:
//    m_db.ConnectTo(MariaDB::MariaDBFactory().Create("127.0.0.1:3306", "user", "password"), "mydatabase");
...
}

To learn more about connecting to SQL databases, have a look at the Framework - CDP2SQL manual.

UI for industrial IO HW video tutorials Adding User Authentication to Automation System

The content of this document is confidential information not to be published without the consent of CDP Technologies AS.

CDP Technologies AS, www.cdpstudio.com

Get started with CDP Studio today

Let us help you take your great ideas and turn them into the products your customer will love.

Try CDP Studio for free
Why CDP Studio?

CDP Technologies AS
Hundsværgata 8,
P.O. Box 144
6001 Ålesund, Norway

Tel: +47 990 80 900
E-mail: info@cdptech.com

Company

About CDP

Contact us

Services

Partners

Blog

Developers

Get started

User manuals

Support

Document download

Release notes

My account

Follow CDP

  • LinkedIn
  • YouTube
  • GitHub

© Copyright 2025 CDP Technologies. Privacy and cookie policy.

Return to top