Database Class
(CDP2SQL::Database)Used for connecting to a database and executing simple commands and queries. More...
| Header: | #include <Database> |
| Inherits: | HasDatabaseConnection |
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, but similar code works also for MySQL and PostgreSQL when using the appropriate factory class (see the Framework - CDP2SQL page).
#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.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.