Use the External Database Connector
Introduction
Use the External Database Connector to connect, retrieve, and insert data into your Mendix app.
The External Database Connector supports connections to the following databases:
- MSSQL
- MySQL
- PostgreSQL
- Oracle
- Snowflake (GA support from Studio Pro 10.12 – Beta versions are available from Studio Pro 10.10)
This how-to teaches you how to do the following:
- Connect your Mendix App to an external database
- Create and validate SQL Queries
- Use created queries in the Query External Database activity
For information on how to configure the connector, see External Database Connector. For information about the database wizard and how to connect using the External Database Connection document, see External Database Connection.
Prerequisites
Download the External Database Connector into your app. Make sure you have the following details for your external connection:
- Username and Password for signing into the external database
- Connection details: Host, Port, Database name
If additional connection properties are required to connect, you can alternatively use JDBC Connection String.
Connect to the External Database
Establish Connection Between the External Database and Mendix App
-
Right-click the module you want to add the external database document to and click Add other > External database connection.
-
Select the database you want to connect to and add the connection details the Database Connection wizard.
-
Click “Test Connection” to validate the connection to the external database.
Click Save to save the connection details, which are stored in 3 constants:
\<Document Name\>_DBSource
\<Document Name\>_DBUsername
\<Document Name\>_DBPassword
For example: *Database*_DBsource.
Explore Schemas of a Connected Database
When the connection is successful and saved, you can search the Browse database tab for Tables, Views, Procedures, and Functions.
Create and Validate SQL Queries with Parameters
-
Create a new query by entering an SQL query in the SQL query field.
-
Open the Parameters tab and click Add Parameter if you want to use constants or variables in your query during runtime. Use curly braces to include a parameter in the query.
-
Assign a Test Value to each parameter.
-
Click Run Query to validate the query and view the response.
For example, the query below retrieves a list of RequestedProductRequirement where the ProductLine is Planes.
SQL Query:
Select requestedProductRequirement from productlines where productLine = {productLine}
Typecast Parameter Data Type
You can typecast String
into UUID as shown below:
Save Query
Save Query to Retrieve Data
-
Click Use Response to view the response data and mapping.
-
In the Response Structure tab, you can choose create New Entity or Reuse Existing Entity.
-
If New Entity is selected, you can view the entity in the Response tab. Click Save Query & Create Entity to save the query and the newly created entity in the domain model.
-
If Reuse Entity is selected, all entities mapped to other queries of same document are listed in the drop down list. Select the entity you want to reuse and and click on Save Query.
Save DML Query
-
For DML queries, Number of affected rows will be displayed as a response.
For example,
INSERT INTO classicmodels.productlines(productLine, requestedProductRequirement)VALUES({productLine}, {requestedProductRequirement})
-
Click Save Query.
Update Existing Query
It is possible to use the existing entity when updating a existing query.
For example, you can modify the query below to retrieve a list of productLine, textDescription, htmlDescription columns from ProductLines where the ProductLine is Planes.
SQL Query:
Existing Query: Select requestedProductRequirement from productlines where productLine = {productLine}
Modified Query: Select productLine, textDescription, htmlDescription from productlines where productLine = {productLine}
Do the following:
-
Rerun the Query, to see the Response.
-
Click Use Response to see the entity preview.
-
Use the existing entity or create a new entity:
-
If New Entity is selected, you can view the entity in the Response tab.
- Click Save Query & Create Entity to save the query and the newly created entity in the domain model.
-
If Update Entity is selected,you can see changes that will be made to the existing entity.
- Click Update Entity to save the query and the changes made to the entity in the domain model.
Call Stored Procedure
To call a stored procedure, do the following:
-
Select the Stored procedure checkbox.
-
Enter the query to call a stored procedure. Add a schema name with the stored procedure name. For example, use the following syntax, where
latest_schema
is the schema name, andInsertDataIntoTable1
is the stored procedure:Call latest_schema.InsertDataIntoTable1({1},{2})
. -
Create IN, OUT, and INOUT parameters for all parameters present in the stored procedure. Make sure the Name in DB is the same as the name of parameter in the stored procedure.
-
Click Run Query. This returns an entity with the number of affected rows and all INOUT and OUT parameters. If the stored procedure returns a Result set, an associated entity is created.
-
Click Use Response > Save Query & Create Entity to save the query and the newly-created entities in the domain model.
For Postgres, Mendix supports the following parameters:
- Decimal/numeric
- Real
- Double Precision
- Big Serial
- Small Serial
- Serial
- Bpchar
- Char
- Varchar
- Text
- Integer
- Smallint
- Bigint
- Timestamp with timezone
- Date only
- Time without timezone
- Time with timezone
For MSSQL, for INOUT and OUT parameters of type Decimal, test values (in design time) are rounded off.
Use the Query External Database Activity
-
Add the Query external database activity into your microflow.
-
Double-click on the activity and select the new external database document.
-
In the Query field, select the required query from the drop-down.
-
Assign values to the parameters using the Expression editor.
-
Output details for the selected query auto-populate.
You are now ready to use data from an external database in your Mendix App.