Query Class
(CDP2SQL::Query)Class that is used for making queries to the database. More...
Header: | #include <CDP2SQL/Query.h> |
Inherits: | HasDatabaseConnection |
Public Functions
Query() | |
Query(Database &db) | |
Query(Database &db, const std::string &statementString) | |
Query(Database &db, Statement &statement) | |
~Query() override | |
void | Close() |
void | ExecuteQuery(const std::string &queryString) |
void | ExecuteQuery(Statement &st) |
std::string | FieldName(int field) const |
std::string | FieldType(int field) const |
const unsigned char * | FieldValueBlob(int field, int &len) const |
bool | FieldValueBool(int field) const |
double | FieldValueDouble(int field) const |
int64_t | FieldValueInt64(int field) const |
int | FieldValueInt(int field) const |
std::string | FieldValueStr(int field) const |
std::time_t | FieldValueTime(int field) const |
bool | IsEof() |
bool | IsFieldValueNull(int field) const |
bool | NextRow() |
unsigned int | NumFields() const |
- 2 public functions inherited from CDP2SQL::HasDatabaseConnection
Detailed Description
Class that is used for making queries to the database.
It allows you to iterate over the results, using a "cursor".
Example usage:
#include <CDP2SQL/CDP2SQL.h> #include <CppSQLite3/SQLite3Factory.h> ... using namespace CDP2SQL; try { Database db(SQLite3Factory().Create(), "mysqlite3databasefile.db"); Query q(db, "select id, fullname, salary from employee;"); while (!q.IsEof()) { int id = q.FieldValueInt(0); std::string name = q.FieldValueStr(1); double pay = q.FieldValueDouble(2); q.NextRow(); } } catch (const SQLException& e) { printf("Error occurred: %s\n", e.Message()); }
Member Function Documentation
Query::Query()
Default constructor. Only use this if you have to delay connecting it to a Database object.
Query::Query(Database &db)
Constructor taking a Database reference. Must be connected.
Query::Query(Database &db, const std::string &statementString)
Constructor taking a Database reference and an SQL statement string. Executes the SQL statement immediately.
Query::Query(Database &db, Statement &statement)
Constructor taking a Database reference and a Statement object. Executes the Statement immediately.
The Statement object must be precompiled and have any placeholders bound to values.
Query::~Query()
Destroys the instance of Query.
void Query::Close()
Closes and finalizes the query. Done automatically by the destructor.
void Query::ExecuteQuery(const std::string &queryString)
Executes an SQL query string.
void Query::ExecuteQuery(Statement &st)
Executes an SQL query using the given Statement.
The Statement must be ready for use (unused or Reset, and any placeholder binded)
std::string Query::FieldName(int field) const
Name of field in column field. The numbering starts with 0
.
std::string Query::FieldType(int field) const
Type of field in column field. The numbering starts with 0
.
const unsigned char *Query::FieldValueBlob(int field, int &len) const
Gets blob value and length.
Pointer is valid until calling NextRow(), Close() or ExecuteQuery(). The numbering starts with 0
.
bool Query::FieldValueBool(int field) const
Gets value in column field as boolean. The numbering starts with 0
.
Returns false
if field is NULL or 0. Otherwise true
.
double Query::FieldValueDouble(int field) const
Gets value in column field as floating point. The numbering starts with 0
.
int64_t Query::FieldValueInt64(int field) const
Gets value in column field as int64_t. The numbering starts with 0
.
int Query::FieldValueInt(int field) const
Gets value in column field as integer. The numbering starts with 0
.
std::string Query::FieldValueStr(int field) const
Gets value in column field as string. The numbering starts with 0
.
std::time_t Query::FieldValueTime(int field) const
Gets the date value in column field as a time_t type. The numbering of field starts with 0
.
It assumes it was stored in the database in proper ISO-8601 format (YYYY-MM-DD HH:MM:SS)
Note: Information about timezone, DST etc will be lost.
bool Query::IsEof()
Returns true
if we have reached the End-Of-File for the results.
bool Query::IsFieldValueNull(int field) const
Returns true
if the field value is a SQL NULL. The numbering starts with 0
.
bool Query::NextRow()
Goes to the next row. Returns false
if there is no next row in the result.
unsigned int Query::NumFields() const
Returns how many fields (columns) the result has.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.