hub.eb?material_id=416&track_id=414

Database Services


The services.database interface contains functions for retrieving and updating database rows. These operations are performed directly, without the need for a Database Resource.

The available functions include:

  • executeSelectStatement(), to execute an SQL statement to retrieve data from the specified database. Each row is passed, in turn, to a callback function.
  • executeGenericUpdateStatement(), to update the specified database using a SQL statement. This function can be used to change database structure or to insert, delete or update table rows.

In both of the above functions, a database connection name is passed as a parameter. This must match a database connection that has been configured using the Server Admin App.

The interface also includes additional functionality for working with Mongo (a non-relational database).

Steps


1

Create a form. Add a Button Control and a Text Control, named myTxt, to your page.

2

Create a new script to handle the button's On Click event. Copy the following code:

// Database connection, defined in the Server Admin App
var dbConn = "EBASE_SAMPLES";

// Select statement SQL to retrieve all rows from the "PETS" sample database.
var selectStatement = "SELECT * FROM PETS";

// Define an array to store the pet names in
var petNames = [];

// Execute the select statement SQL
services.database.executeSelectStatement(dbConn, selectStatement, function(data) {
  // Store the value of the "NAME" field for the current row
  petNames.push(data.NAME);
});

// Display the list of pet names using the Text Control
controls.myTxt.text.setText(petNames.join(", "));
3

Run the form and click the button to execute the select statement. A list of pet names that have been retrieved from the database will be shown in the Text Control.

Current Module

Related