• 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

  • Framework - CDP2SQL
  • Database
  • 5.0.0

Database Class

(CDP2SQL::Database)

Used for connecting to a database and executing simple commands and queries. More...

Header: #include <Database>
Inherits: HasDatabaseConnection
  • List of all members, including inherited members

Public Functions

Database()
Database(IDatabaseConnection *dbType, const std::string &connectionString, void *connectionInfo = 0)
~Database() override
int ConnectTo(IDatabaseConnection *dbConnection, const std::string &database = "", void *connectionInfo = 0)
void Disconnect()
void Execute(const std::string &sqlString)
int ExecuteScalar(const std::string &sqlString)
std::string GetDBType() const
void Interrupt()
bool TableExists(const std::string &tableName)
bool TestConnection()
void WalCheckpoint()
  • 2 public functions inherited from CDP2SQL::HasDatabaseConnection

Detailed Description

Used for connecting to a database and executing simple commands and queries.

The examples here are made using SQLite.

#include <CDP2SQL/CDP2SQL.h>
#include <CppSQLite3/SQLite3Factory.h>

...

using namespace CDP2SQL;
try {
  Database db(SQLite3Factory().Create(), "mysqlite3databasefile.db");
  db.Execute("insert into employee values (99, 'John Doe');" );
  int nEmployees = db.ExecuteScalar("select count(*) from employee;");
} catch (const SQLException& e) {
  printf("Error occurred: %s\n", e.Message());
}

For more complicated queries, see the CDP2SQL::Query class which provides the means for iterating over the results.

For insertions and updates, see the CDP2SQL::Statement class for parameterizing of the arguments and performance improvement of repeated actions.

Member Function Documentation

Database::Database()

Construct a database holder object.

You will have to call ConnectTo before you can use this object.

Database::Database(IDatabaseConnection *dbType, const std::string &connectionString, void *connectionInfo = 0)

Construct a database holder object.

Utility version that calls ConnectTo directly. Throws CDP2SQL::SQLException if unsupported parameters or unexpected errors occur.

Database::~Database()

Destroys the instance of Database.

int Database::ConnectTo(IDatabaseConnection *dbConnection, const std::string &database = "", void *connectionInfo = 0)

Connects to a database.

Example:

db.ConnectTo(SQLite3Factory().Create(), "path/to/mydatabasefile.db");

Parameters:

  • dbConnection - Pointer to a database connection object. Should not be connected yet. This function takes ownership of the pointer.
  • database - Database to open after connecting (optional). For SQLite this is also the file name (or path).
  • connectionInfo - Pointer to very database-specific configuration setup. Normally not needed.

Throws CDP2SQL::SQLException if unsupported parameters or unexpected errors occur.

Returns a positive value on success, which may include database-specific information. In case SQLite, 1 is returned if existing file was opened, 2 if new file was created.

void Database::Disconnect()

Disconnects from the database and closes the connection.

void Database::Execute(const std::string &sqlString)

Executes a SQL data manipulation command.

Example:

db.Execute( "insert into mytable values (1, "foobar);" );

int Database::ExecuteScalar(const std::string &sqlString)

Executes a simple SQL query that returns a single scalar value.

Example:

db.ExecuteScalar( "select count(*) from mytable;" );

Returns the single integer result from the query.

std::string Database::GetDBType() const

Database type as a string (e.g. "sqlite3" or "mysql")

void Database::Interrupt()

Asks any pending database operation to abort and return at its earliest opportunity.

It is safe to call this routine from a thread different from the thread that is currently running the database operation.

bool Database::TableExists(const std::string &tableName)

Returns true if the database has a table with this name.

bool Database::TestConnection()

Pings the database to check if the connection is still up. Returns whether the Database is connected.

void Database::WalCheckpoint()

Causes checkpoint/sync on databases that supports Write-Ahead Logging.

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