Storing Device Data
Currently, as part of W3bstream's DevNet release, each W3bstream project is equipped with a dedicated SQL database. This database serves as a valuable resource for storing machine data, application status, and more.
However, it's important to note that the SQL database solution is temporary and intended for the W3bstream DevNet phase. In the future, developers will have more options to store IoT data in a variety of locations, including decentralized storage systems. W3bstream will provide storage modules that enable applets to seamlessly access and interact with data stored in these storage environments.
This future enhancement will empower developers to leverage the benefits of the different storage solutions, while maintaining the flexibility and scalability needed for IoT applications.
Using the SQL database
When you create a project in W3bstream, a dedicated SQL database is automatically created and made accessible to the project. This SQL database allows you to store and retrieve structured data for your project. To interact with the SQL database from your Applet, the SDKs provide two functions: ExecSQL
and QuerySQL
.
ExecSQL
The ExecSQL
function is designed for executing queries that modify the database rows, such as inserting, deleting, or altering rows. It returns an error code or 0 on success. You can use ExecSQL
to perform operations that modify the database structure or content.
QuerySQL
On the other hand, the QuerySQL
function is suitable for running queries that retrieve data from the database. It is used when you expect the query to return a certain number of rows. You can use QuerySQL
to fetch data from the SQL database based on specific conditions or criteria.
Example: Storing and Retrieving Data
Before you can start using the database, it's important to create the necessary tables to store your data. In our example, we'll be using the payload discussed in the previous sectionDefining the message protocol. From the Data section of the project editor in W3bstream Studio, we'll create a database table named "iot_data
" with three columns: "device_id
" (string), "temperature
" (float64), and "timestamp
" (string).
To learn how to create database tables, you can watch the short video tutorial in the how-to section:
Create Database TablesLet's look at an example that demonstrates storing a record in the SQL database and reading data back from it:
Last updated