Skip to content

Latest commit

 

History

History
 
 

querying-a-database

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Queries

The information you want to present in any application is typically spread over numerous tables in a well-designed database. A query can collect data from different tables/ documents and pull it together for display. You can either request data from your database or manipulate the data, or both.

{% hint style="info" %} To write and execute queries, you should understand the basics of connecting to a Database or Connecting to an API. {% endhint %}

A query can insert, update, or remove data from a database, conduct calculations, integrate data from other databases, and much more.

{% hint style="info" %} APIs, Database Queries, and Integrations to perform CRUD operations on data are collectively termed Queries in Appsmith**.** {% endhint %}

You can create queries to manipulate data from a datasource using the Appsmith query editor.

{% hint style="info" %} By default, the prepared statement is enabled for all queries. Prepared statements provide a secure way of executing your queries. You can read How to Use Prepared Statements for efficient and secure data transactions. {% endhint %}

Setting up a Query

Go to the page where you wish to run the query. Follow the below steps to set up a query using a query editor:

{% hint style="success" %} Queries are auto-saved, so you never lose your work. These changes will be reflected in your published application only after you deploy. {% endhint %}

{% embed url="https://youtu.be/N6zRxIVSGfk" %} How to set up a Query? {% endembed %}

  • Go to Datasources → + to create a new query or select an existing datasource to which you wish to add a query.

{% hint style="info" %} You can follow the steps detailed in Datasources to add a new datasource. {% endhint %}

  • Choose the connected datasource and click + New Query. You will be taken to the query editor to configure the query. You can also choose to add a new query by navigating to Explorer >> Scroll down to Queries/JS >> Click plus (+) sign >> Select the name <DATASOURCE_NAME> Query (users query)

Create a Query from QUERIES/JS Menu Item

{% hint style="info" %} You can configure your API(s) using a REST interface. {% endhint %}

{% hint style="danger" %} The query is created with a default name. It’s recommended that you rename it for readability and access. {% endhint %}

  • Write your query in the syntax that is valid for your database type.
  • Bind your query to the widget and carry out data manipulations.

{% hint style="warning" %} A query and its results can be accessed from only the page it is a part of. To use a query on another page, click the context menu next to the query name and clone it. {% endhint %}

Naming a Query

A query must have a unique and meaningful name that acts as an identifier. It is used to access the query results. In that sense, a name is like a variable in a programming language. You can access the various properties of the query and its data using the query name.

{% hint style="warning" %} Note that JavaScript keywords and the window object methods and properties are not valid as query names. {% endhint %}

Running a Query

Click on the Run button or hit cmd + enter to execute a query. You'll see the results in the Response tab if the query succeeds.

{% embed url="https://youtu.be/0xA7ChO7Rlk" %} How to run a query? {% endembed %}

All query results are stored in the data property of the query object. It is immutable and cannot be changed. You can access it using JavaScript, as shown in the code snippet below:

{{ Query1.data }}

{% hint style="info" %} DB Queries fetch data in the form of an array of objects where each object is a row, and each key in the object is a column. {% endhint %}

All API Queries return the exact API response of the endpoint.

Using Queries in applications

Once you have successfully run a Query, you can use it in your application to