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.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.