> Documentation index: https://firebase.google.com/docs/reference/cpp/namespace/firebase/llms.txt

# firebase::database

Namespace for the Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) C++ SDK.

## Summary

| ### Enumerations ||
|---|---|
| `Error{ kErrorNone = 0, kErrorDisconnected, kErrorExpiredToken, kErrorInvalidToken, kErrorMaxRetries, kErrorNetworkError, kErrorOperationFailed, kErrorOverriddenBySet, kErrorPermissionDenied, kErrorUnavailable, kErrorUnknownError, kErrorWriteCanceled, kErrorInvalidVariantType, kErrorConflictingOperationInProgress, kErrorTransactionAbortedByUser }` | enumError code returned by Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) C++ functions. |
| `TransactionResult{ kTransactionResultSuccess, kTransactionResultAbort }` | enumSpecifies whether the transaction succeeded or not. |

| ### Typedefs ||
|---|---|
| `DoTransaction)(MutableData *data)` | typedef `TransactionResult(*` Your own transaction handler, which the Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) library may call multiple times to apply changes to the data, and should return success or failure depending on whether it succeeds. |
| `DoTransactionFunction` | typedef `std::function< TransactionResult(MutableData *data)>` Your own transaction handler function or lambda, which the Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) library may call multiple times to apply changes to the data, and should return success or failure depending on whether it succeeds. |
| `DoTransactionWithContext)(MutableData *data, void *context)` | typedef `TransactionResult(*` Your own transaction handler, which the Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) library may call multiple times to apply changes to the data, and should return success or failure depending on whether it succeeds. |

| ### Functions ||
|---|---|
| `GetErrorMessage(Error error)` | `const char *` Get the human-readable error message corresponding to an error code. |
| `ServerTimestamp()` | `const Variant &` Get a server-populated value corresponding to the current timestamp. |
| `operator==(const DatabaseReference & lhs, const DatabaseReference & rhs)` | `bool` Compares two [DatabaseReference](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database-reference.md.txt#classfirebase_1_1database_1_1_database_reference) instances. |
| `operator==(const Query & lhs, const Query & rhs)` | `bool` Compares two [Query](https://firebase.google.com/docs/reference/cpp/class/firebase/database/query.md.txt#classfirebase_1_1database_1_1_query) instances. |

| ### Classes ||
|---|---|
| [firebase::database::ChildListener](https://firebase.google.com/docs/reference/cpp/class/firebase/database/child-listener.md.txt) | Child listener interface. |
| [firebase::database::DataSnapshot](https://firebase.google.com/docs/reference/cpp/class/firebase/database/data-snapshot.md.txt) | A [DataSnapshot](https://firebase.google.com/docs/reference/cpp/class/firebase/database/data-snapshot.md.txt#classfirebase_1_1database_1_1_data_snapshot) instance contains data from a Firebase [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) location. |
| [firebase::database::Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt) | Entry point for the Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) C++ SDK. |
| [firebase::database::DatabaseReference](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database-reference.md.txt) | [DatabaseReference](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database-reference.md.txt#classfirebase_1_1database_1_1_database_reference) represents a particular location in your [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) and can be used for reading or writing data to that [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) location. |
| [firebase::database::DisconnectionHandler](https://firebase.google.com/docs/reference/cpp/class/firebase/database/disconnection-handler.md.txt) | Allows you to register server-side actions to occur when the client disconnects. |
| [firebase::database::MutableData](https://firebase.google.com/docs/reference/cpp/class/firebase/database/mutable-data.md.txt) | Instances of this class encapsulate the data and priority at a location. |
| [firebase::database::Query](https://firebase.google.com/docs/reference/cpp/class/firebase/database/query.md.txt) | The [Query](https://firebase.google.com/docs/reference/cpp/class/firebase/database/query.md.txt#classfirebase_1_1database_1_1_query) class is used for reading data. |
| [firebase::database::ValueListener](https://firebase.google.com/docs/reference/cpp/class/firebase/database/value-listener.md.txt) | Value listener interface. |

## Enumerations

### Error

```c++
 Error
```
Error code returned by Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) C++ functions.

| Properties ||
|---|---|
| `kErrorConflictingOperationInProgress` | An operation that conflicts with this one is already in progress. For example, calling SetValue and SetValueAndPriority on a [DatabaseReference](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database-reference.md.txt#classfirebase_1_1database_1_1_database_reference) is not allowed. |
| `kErrorDisconnected` | The operation had to be aborted due to a network disconnect. |
| `kErrorExpiredToken` | The supplied auth token has expired. |
| `kErrorInvalidToken` | The specified authentication token is invalid. |
| `kErrorInvalidVariantType` | You specified an invalid [Variant](https://firebase.google.com/docs/reference/cpp/class/firebase/variant.md.txt#classfirebase_1_1_variant) type for a field. For example, a [DatabaseReference](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database-reference.md.txt#classfirebase_1_1database_1_1_database_reference)'s Priority and the keys of a Map must be of scalar type (MutableString, StaticString, Int64, Double). |
| `kErrorMaxRetries` | The transaction had too many retries. |
| `kErrorNetworkError` | The operation could not be performed due to a network error. |
| `kErrorNone` | The operation was a success, no error occurred. |
| `kErrorOperationFailed` | The server indicated that this operation failed. |
| `kErrorOverriddenBySet` | The transaction was overridden by a subsequent set. |
| `kErrorPermissionDenied` | This client does not have permission to perform this operation. |
| `kErrorTransactionAbortedByUser` | The transaction was aborted, because the user's DoTransaction function returned kTransactionResultAbort instead of kTransactionResultSuccess. |
| `kErrorUnavailable` | The service is unavailable. |
| `kErrorUnknownError` | An unknown error occurred. |
| `kErrorWriteCanceled` | The write was canceled locally. |

### TransactionResult

```c++
 TransactionResult
```
Specifies whether the transaction succeeded or not.

| Properties ||
|---|---|
| `kTransactionResultAbort` | The transaction did not succeed. Any changes to the [MutableData](https://firebase.google.com/docs/reference/cpp/class/firebase/database/mutable-data.md.txt#classfirebase_1_1database_1_1_mutable_data) will be discarded. |
| `kTransactionResultSuccess` | The transaction was successful, the [MutableData](https://firebase.google.com/docs/reference/cpp/class/firebase/database/mutable-data.md.txt#classfirebase_1_1database_1_1_mutable_data) was updated. |

## Typedefs

### DoTransaction

```c++
TransactionResult(* DoTransaction)(MutableData *data)
```
Your own transaction handler, which the Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) library may call multiple times to apply changes to the data, and should return success or failure depending on whether it succeeds.

<br />

> [!NOTE]
> **Note:** This version of the callback is no longer supported. You should use either one of DoTransactionWithContext (a simple function pointer that accepts context data) or DoTransactionFunction (based on std::function).

**See also:** [DoTransactionWithContext](https://firebase.google.com/docs/reference/cpp/namespace/firebase/database.md.txt#namespacefirebase_1_1database_1a2304afa2b9c5965fb7a1f736ab95bf86) for more information.

<br />

### DoTransactionFunction

```c++
std::function< TransactionResult(MutableData *data)> DoTransactionFunction
```
Your own transaction handler function or lambda, which the Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) library may call multiple times to apply changes to the data, and should return success or failure depending on whether it succeeds.

**See also:** [DoTransactionWithContext](https://firebase.google.com/docs/reference/cpp/namespace/firebase/database.md.txt#namespacefirebase_1_1database_1a2304afa2b9c5965fb7a1f736ab95bf86) for more information.

### DoTransactionWithContext

```c++
TransactionResult(* DoTransactionWithContext)(MutableData *data, void *context)
```
Your own transaction handler, which the Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) library may call multiple times to apply changes to the data, and should return success or failure depending on whether it succeeds.

The context you specified to RunTransaction will be passed into this call.

This function will be called, *possibly multiple times*, with the current data at this location. The function is responsible for inspecting that data and modifying it as desired, then returning a TransactionResult specifying either that the [MutableData](https://firebase.google.com/docs/reference/cpp/class/firebase/database/mutable-data.md.txt#classfirebase_1_1database_1_1_mutable_data) was modified to a desired new state, or that the transaction should be aborted. Whenever this function is called, the [MutableData](https://firebase.google.com/docs/reference/cpp/class/firebase/database/mutable-data.md.txt#classfirebase_1_1database_1_1_mutable_data) passed in must be modified from scratch.

Since this function may be called repeatedly for the same transaction, be extremely careful of any side effects that may be triggered by this function. In addition, this function is called from within the Firebase Realtime [Database](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database.md.txt#classfirebase_1_1database_1_1_database) library's run loop, so care is also required when accessing data that may be in use by other threads in your application.

Best practices for this function are to ONLY rely on the data passed in.


> [!NOTE]
> **Note:** If you want a callback to be triggered when the transaction is finished, you can use the Future value returned by the method running the transaction, and call [Future::OnCompletion()](https://firebase.google.com/docs/reference/cpp/class/firebase/future.md.txt#classfirebase_1_1_future_1a334de3fc89aefc4704c89f3fb7ba1c08) to register a callback to be called when the transaction either succeeds or fails.

<br />

Details Parameters `data` Mutable data, which the callback can edit. `context` Context pointer, passed verbatim to the callback. **Returns** The callback should return kTransactionResultSuccess if the data was modified, or kTransactionResultAbort if it was unable to modify the data. If the callback returns kTransactionResultAbort, the RunTransaction() call will return the kErrorTransactionAbortedByUser error code.

## Functions

### GetErrorMessage

```c++
const char * GetErrorMessage(
  Error error
)
```
Get the human-readable error message corresponding to an error code.


Details Parameters `error` Error code to get the error message for. **Returns** Statically-allocated string describing the error.

### ServerTimestamp

```c++
const Variant & ServerTimestamp()
```
Get a server-populated value corresponding to the current timestamp.

When inserting values into the database, you can use the special value [firebase::database::ServerTimestamp()](https://firebase.google.com/docs/reference/cpp/namespace/firebase/database.md.txt#namespacefirebase_1_1database_1a77f136a633fb85b06e5b3f56400fa21a) to have the server auto-populate the current timestamp, which is represented as millieconds since the Unix epoch, into the field.


| Details ||
|---|---|
| **Returns** | A special value that tells the server to use the current timestamp. |

### operator==

```c++
bool operator==(
  const DatabaseReference & lhs,
  const DatabaseReference & rhs
)
```
Compares two [DatabaseReference](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database-reference.md.txt#classfirebase_1_1database_1_1_database_reference) instances.


Details Parameters `lhs` A [DatabaseReference](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database-reference.md.txt#classfirebase_1_1database_1_1_database_reference). `rhs` A [DatabaseReference](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database-reference.md.txt#classfirebase_1_1database_1_1_database_reference) to compare against. **Returns** True if the [DatabaseReference](https://firebase.google.com/docs/reference/cpp/class/firebase/database/database-reference.md.txt#classfirebase_1_1database_1_1_database_reference) instances have the same URL. False otherwise.

### operator==

```c++
bool operator==(
  const Query & lhs,
  const Query & rhs
)
```
Compares two [Query](https://firebase.google.com/docs/reference/cpp/class/firebase/database/query.md.txt#classfirebase_1_1database_1_1_query) instances.

Two [Query](https://firebase.google.com/docs/reference/cpp/class/firebase/database/query.md.txt#classfirebase_1_1database_1_1_query) instances on the same database, in the same location, with the same parameters (OrderBy\*, StartAt, EndAt, EqualTo, Limit\*) are considered equivalent.

Equivalent Queries have a shared pool of ValueListeners and ChildListeners. When listeners are added or removed from one [Query](https://firebase.google.com/docs/reference/cpp/class/firebase/database/query.md.txt#classfirebase_1_1database_1_1_query) instance, it affects all equivalent [Query](https://firebase.google.com/docs/reference/cpp/class/firebase/database/query.md.txt#classfirebase_1_1database_1_1_query) instances.


Details Parameters `lhs` The [Query](https://firebase.google.com/docs/reference/cpp/class/firebase/database/query.md.txt#classfirebase_1_1database_1_1_query) to compare against. `rhs` The [Query](https://firebase.google.com/docs/reference/cpp/class/firebase/database/query.md.txt#classfirebase_1_1database_1_1_query) to compare against. **Returns** True if the [Query](https://firebase.google.com/docs/reference/cpp/class/firebase/database/query.md.txt#classfirebase_1_1database_1_1_query) instances have the same database, the same path, and the same parameters, determined by StartAt(), EndAt(), EqualTo(), and the OrderBy and LimitTo methods. False otherwise.