# Data Queries

Queries support you to read data from or write data to your data sources. You can create queries while editing an app or in the query library. This tutorial walks you through how to create a query and tailor it to your needs.

### DbBuilder

The UI of query editor varies when you choose different types of data sources. Below is an example of a connection to a DbBuilder and a corresponding statement.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FxI8y6lNKZU6WsmPnVDfq%2Fimage.png?alt=media&#x26;token=0c0f61ff-684c-42cb-89e4-12dd08f740d3" alt=""><figcaption></figcaption></figure>

#### Choose Data Source

Select the datasource from the list - or - create a new datasource and attach.

#### Trigger when

Appizap triggers your queries in two modes, either run automatically when inputs change or on page load, or manually invoked in event handlers.

#### Trigger when - Inputs change or on page load

Queries set to this mode automatically run when dependent inputs change or on page load. For example, the query result of `select * from users where customer_id = {{input.value}}` updates immediately when `input.value` changes. \*\*\*\* This mode to run a query is recommended for queries reading data from data sources.

#### Trigger when - Manually invoked

For this mode of queries, they run only with your manual trigger, such as a button or link click. You need to specify an event handler to trigger the query. This mode is recommended for queries writing data to data sources, because you may need to confirm that the input is complete and error-free before running it.

{% hint style="info" %}
**How to choose a query's trigger mode?**

In most cases, queries reading data, like **select** operations, can be set to the former mode, and those writing data, like **create**/**delete**/**update** operations, run the latter way.

When Appizap detects your query statement switches from reading to writing data, the trigger mode switches to **Manually invoked** automatically, but not the other way around.
{% endhint %}

#### Table

The display provides a comprehensive view of all the tables accessible within the DbBuilder source. Users have the option to select a specific table from the displayed list of tables.

#### Actions

| Property   | Description                                                                                                                                                                                                                                                                                                               |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Create row | To insert a new record into the table picked. User needs to enter the values for each column of the table.                                                                                                                                                                                                                |
| Update row | To update an existing row of the table. The user needs to provide the id value and values for the other columns of the table.                                                                                                                                                                                             |
| Delete row | To delete one or more rows from the selected table, the user can set the criteria using the input fields shown below this field and the records matching the criteria get deleted.                                                                                                                                        |
| List       | <p>To retrieve the existing records from the table, the user can set the criteria using the input fields shown below this field and the records matching the requirements get pulled.<br><br>The "Limit" field permits the user to input a numerical value, and the query will fetch that specific number of records.</p> |

#### Custom Query

Appizap evaluates your query statement with JavaScript code inside `{{ }}` in real-time, and the result is displayed below in a floating box, so you can use it to check the correctness of the query statement.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FgWqRMwG80SHADjk5DYDO%2Fimage.png?alt=media&#x26;token=e07ea576-b72d-41e6-91ae-11617900f790" alt=""><figcaption></figcaption></figure>

#### Event Handlers

The user can activate an action following the execution of the query event. There are two possible outcomes: 'success' and 'failure', with the action being triggered accordingly.

## Notification & Advanced Tabs

You can configure settings regarding notifications, timeout, periodic run, and more. Settings in **Notification** tab and **Advanced** vary based on trigger mode of your query.

| Tab              | Settings                                    | Inputs change or on page load | Manually invoked |
| ---------------- | ------------------------------------------- | :---------------------------: | :--------------: |
| **Notification** | Display a Success message after running     |               🚫              |         ✅        |
|                  | Display a Failure message after running     |               ✅               |         ✅        |
| **Advanced**     | Display a confirmation modal before running |         <p>🚫<br></p>         |         ✅        |
|                  | Set timeout for query running               |               ✅               |         ✅        |
|                  | Perform query periodically                  |               ✅               |        🚫        |
