Tutorials References Exercises Videos Menu
Create Website Get Certified Upgrade

AppML Reference - Databases


The "database" Property

The "database" property defines a database as the data source. It has the following sub properties:

Element Description
"connection" The name of a database connection
"execute" Array of SQL statements to be executed before data retrieval (optional)
"keyfield" The key field for the main table (optional)
"maintable" The main table for this application (optional)
"orderby" A fixed SQL orderby clause for the application (optional)
"sql" The SQL statement for retrieving data

Data From a Database

This model fetches records containing Customer, City, and Country from a Customer table in an SQL database:

Example

{
"database": {
    "connection": "mysql",
    "sql"       : "SELECT CustomerName, City, Country FROM Customers",
    "orderby"   : "CustomerName"
}
}

Filter Restrictions

To allow users to filter data, you can add filter information to the model:

"filteritems" : [
    {"item" : "CustomerName", "label" : "Customer"},
    {"item" : "City"},
    {"item" : "Country"}]

Sorting Restrictions

To allow users to sort data, you can add sort information to the model:

"sortitems" : [
    {"item" : "CustomerName", "label" : "Customer"},
    {"item" : "City"},
    {"item" : "Country"}]

Update Restrictions

To allow users to update data, you can include update information in the model: 

Example

"updateItems" : [
    {"item" : "CustomerName"},
    {"item" : "Address"},
    {"item" : "PostalCode"},
    {"item" : "City"},
    {"item" : "Country"}]

By default, AppML will let you filter, sort, or update data, only it is specified in the model.



Database Connections

Database connections are defined in appml_config.php:

appml_config.php

<?php echo("Access Forbidden");exit();?>
{
"dateformat" : "yyyy-mm-dd",
"databases": [
    {
    "connection" : "mysql",
    "host"       : "127.0.0.1:3306",
    "dbname"     : "Northwind",
    "username"   : "myUserId",
    "password"   : "myPassword"
    },
    {
    "connection" : "googleDB",
    "host"       : "192.168.1.1:3306",
    "dbname"     : "Northwind",
    "username"   : "myUserId",
    "password"   : "myPassword"
    },
    {
    "connection" : "amazonDB",
    "host"       : "mydbinstance.amazon.com:3306",
    "dbname"     : "Northwind",
    "username"   : "myUserId",
    "password"   : "myPassword"
    },
    {
    "connection" : "azureDB",
    "host"       : "azure.cloudapp.net",
    "dbname"     : "Northwind",
    "username"   : "myUserId",
    "password"   : "myPassword"
    }
]
}

The configuration file can contain many database connections.


Creating Databases

Since AppML allows you to execute SQL statements before the application is started, you can use this to create a database if needed:

Model

{
"database" : {
"connection" : "myCDs",
"execute" : [
"DROP TABLE IF EXISTS CD_Catalog",
"CREATE TABLE IF NOT EXISTS CD_Catalog (CDID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (CDID),Title NVARCHAR(255),Artist NVARCHAR(255),Country NVARCHAR(255),Price NUMBER)"
]
}}

Perfect for rapid prototyping!