# openPilotLog: Db Class Reference

Db Class Reference

The Db class provides a basic API for accessing the database programatically. It is used to set up the initial connection and various basic queries can be executed using a set of static functions. When interfacing with the database for the purpose of adding, deleting or updating entries, the use of the entry class and its subclasses is recommended. This clas is not supposed to be instantiated, if you need a database object, use QSqlDatabase::database("qt_sql_default_connection");. More...

`#include <db.h>`

## Public Types

enum | editRole { createNew, editExisting }
| The editRole enum {createNew, editExisting} is used to differentiate between creating a new entry in the database vs editing an existing one.
enum | matchType { exactMatch, partialMatch }
| The matchType enum {exactMatch, partialMatch} is used to determine the matching when using a WHERE sql statement. exactMatch results in a "=" operator, whereas partiasMatch results in a "LIKE" operator.

## Public Member Functions

| Db (Db const &)=delete
void | operator= (Db const &)=delete

## Static Public Member Functions

static Db & | get ()
static void | connect ()
| connect establishes the database connection. Only needs to be called once within the application. Database is available thereafter, objects can be instantiated with QSqlDatabase::database("qt_sql_default_connection") as required.
static void | disconnect ()
| disconnect Closes and removes the default database connection.
static QSqlDatabase | Database ()
| Can be used to access the database connection. More...
static bool | exists (QString column, QString table, QString checkColumn, QString value, Db::matchType match)
| Db::exists checks if a certain value exists in the database with a sqlite WHERE statement. More...
static bool | singleUpdate (QString table, QString column, QString value, QString checkColumn, QString checkvalue, Db::matchType match)
| Db::singleUpdate Updates a single value in the database. Query format: UPDATE table SET column = value WHERE checkcolumn =/LIKE checkvalue. More...
static QString | singleSelect (QString column, QString table, QString checkColumn, QString value, Db::matchType match)
| singleSelect Returns a single value from the database with a sqlite WHERE statement More...
static QVector< QString > | multiSelect (QVector< QString > columns, QString table, QString checkColumn, QString value, Db::matchType match)
| Db::multiSelect Returns multiple values from the database with a sqlite WHERE statement. More...
static QVector< QString > | multiSelect (QVector< QString > columns, QString table)
| Db::multiSelect Returns a complete column(s) for a given table. More...
static QVector< QString > | customQuery (QString query, int returnValues)
| Db::customQuery Can be used to send a complex query to the database. More...

## Detailed Description

The Db class provides a basic API for accessing the database programatically. It is used to set up the initial connection and various basic queries can be executed using a set of static functions. When interfacing with the database for the purpose of adding, deleting or updating entries, the use of the entry class and its subclasses is recommended. This clas is not supposed to be instantiated, if you need a database object, use QSqlDatabase::database("qt_sql_default_connection");.

## Member Function Documentation

## customQuery()

static QVector<QString> Db::customQuery | ( | QString | query,
|  | int | returnValues
| ) |  |

inlinestatic

Db::customQuery Can be used to send a complex query to the database.

Parameters

  query | - the full sql query statement
  returnValues | - the number of expected return values

Returns
  QVector<QString> of results

## Database()

static QSqlDatabase Db::Database | ( |  | ) |

inlinestatic

Can be used to access the database connection.

Returns
  a pointer to the default database connection

## exists()

static bool Db::exists | ( | QString | column,
|  | QString | table,
|  | QString | checkColumn,
|  | QString | value,
|  | Db::matchType | match
| ) |  |

inlinestatic

Db::exists checks if a certain value exists in the database with a sqlite WHERE statement.

Parameters

  table | - Name of the table
  column | - Name of the column
  value | - The value to be checked

Returns

## multiSelect() [1/2]

static QVector<QString> Db::multiSelect | ( | QVector< QString > | columns,
|  | QString | table
| ) |  |

inlinestatic

Db::multiSelect Returns a complete column(s) for a given table.

Parameters

  column | - QVector<QString> Names of the columns to be queried
  table | - QString Name of the table

Returns

## multiSelect() [2/2]

static QVector<QString> Db::multiSelect | ( | QVector< QString > | columns,
|  | QString | table,
|  | QString | checkColumn,
|  | QString | value,
|  | Db::matchType | match
| ) |  |

inlinestatic

Db::multiSelect Returns multiple values from the database with a sqlite WHERE statement.

Parameters

  table | - Name of the table
  columns | - QVector<QString> Names of the columns to be queried
  value | - Identifier for WHERE statement
  checkColumn | - column to match value to
  match | - enum Db::exactMatch or Db::partialMatch

Returns
  QVector<QString>

## singleSelect()

static QString Db::singleSelect | ( | QString | column,
|  | QString | table,
|  | QString | checkColumn,
|  | QString | value,
|  | Db::matchType | match
| ) |  |

inlinestatic

singleSelect Returns a single value from the database with a sqlite WHERE statement

Parameters

  table | - Name of the table
  column | - Name of the column
  value | - Identifier for WHERE statement
  match | - enum Db::exactMatch or Db::partialMatch

Returns
  QString

## singleUpdate()

static bool Db::singleUpdate | ( | QString | table,
|  | QString | column,
|  | QString | value,
|  | QString | checkColumn,
|  | QString | checkvalue,
|  | Db::matchType | match
| ) |  |

inlinestatic

Db::singleUpdate Updates a single value in the database. Query format: UPDATE table SET column = value WHERE checkcolumn =/LIKE checkvalue.

Parameters

  table | Name of the table to be updated
  column | Name of the column to be updated
  checkColumn | Name of the column for WHERE statement
  value | The value to be set
  checkvalue | The value for the WHERE statement
  match | enum Db::exactMatch or Db::partialMatch

Returns
  true on success, otherwise error messages in debug out

The documentation for this class was generated from the following files:

- src/database/db.h
- src/database/db.cpp