Skip to main content

iRidi API. Databases

System and user SQL databases in iRidi Server store information on changes to server variables over time. This information can be queried and used to create graphs and logs

SQLite version: 3.8.7

To create database queries, familiarize yourself with the SQL language.

System database​

Read-only. Populated from server-project tags configured for database storage.

IR.GetDatabase()​

Access the system database as an SQL object to read its data.

SQL object constructor. Creates an SQL object for working with an SQLite database

Syntax

IR.GetDatabase();

InputValueDescription
-
Output
Object[object SQLPrototype]type: Object
object representing the system database

Example

var systembase = IR.GetDatabase();
IR.Log(systembase) // [object SQLPrototype]

Notes

Tip
  • The system database is read-only

User database​

Supports reading, writing, editing, and deletion. Create your own database with the constructor and populate it with SQL object methods.

new SQL()​

SQL object constructor for creating a user SQLite database

Syntax

new SQL();

InputValueDescription
-
Output
Object[object SQLPrototype]type: Object
object representing a user database

Example

var mybase = new SQL();
IR.Log(mybase); // [object SQLPrototype]

Notes

Tip
  • Store the object in a variable to access it

SQL object methods​

To build a user database, create an SQL object and store it in a variable, called database here. The following methods apply:

.Open()​

Open or create a database file

Syntax

sqlobject.Open(file, undeleted);

InputValueDescription
sqlobjecttype: object
object representing a user or system database
undeletedtruetype: boolean
folder type:
false - deletable folder
...\iRidium pro documents\Server\Documents<br />true - nondeletable folder
...\iRidium pro documents\Server\Database\UserDB\
file"MyBase.db"type: string
database filename
Output
-

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db', true);
mybase.Execute("BEGIN");
mybase.Execute('CREATE TABLE Info(ID int, Value float, Name string)');
mybase.Execute("COMMIT");
mybase.Close();
});

Notes

Tip
  • If the file does not exist, it is created in the project's documents folder, for example

C:\Users\User\Documents\iRidium pro documents\Server\Documents\MyBase.db

or

C:\Users\User\Documents\iRidium pro documents\Server\Database\UserDB\MyBase.db

.Execute()​

Perform a database operation. Available operations are described in the SQL language

Syntax

sqlobject.Execute(action);

InputValueDescription
sqlobjecttype: onbject
object representing a user or system database
action"BEGIN"type: string
database operation
Output
-

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db');
mybase.Execute("BEGIN");
mybase.Execute('CREATE TABLE Info(ID INT, Value REAL, Name TEXT)');
mybase.Execute("COMMIT");
mybase.Close();
});

Notes

Tip
  • For fast, correct database writes, use these logical commands:
mybase.Execute("BEGIN");   // начать работу с базой
...
// действия с базой
...
mybase.Execute("COMMIT"); // закончить работу с базой

.Request()​

Query the database with specified selection conditions. Condition syntax is described in the SQL language

Syntax

sqlobject.Request(conditions);

InputValueDescription
sqlobjecttype: onbject
object representing a user or system database
conditions'SELECT * FROM Info WHERE Value database query body
Output
RecordSet Object[object RecordSetPrototype]type: object
RecordSet object containing the query result

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db');
mybase.Execute("BEGIN");
mybase.Execute('CREATE TABLE Info(ID INT, Value REAL, Name TEXT)');
mybase.Execute('INSERT INTO Info(ID, Value, Name) VALUES(1,19.9,"sunny day")');
var RecordSetObj = mybase.Request('SELECT * FROM Info WHERE ID<30');
IR.Log(RecordSetObj.GetColumnName(0)+'\t'+RecordSetObj.GetColumnName(1)+'\t'+RecordSetObj.GetColumnName(2));
mybase.Execute("COMMIT");
mybase.Close();
});

Notes

Tip
  • The query returns a RecordSet object containing the result

.Close()​

Finish working with the database

Syntax

sqlobject.Close();

InputValueDescription
sqlobjecttype: object
object representing a user or system database
Output
-

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db');
mybase.Close();
});

Notes

Tip
  • Before finishing work with the database, save changes with mybase.Execute("COMMIT")

RecordSet object​

A RecordSet object contains records selected from an SQL database by a query:

var RecordSetObj = sqlobject.Request('SELECT <request>');

For example:

// получить все записи из таблицы Info, где поле Value < 100
var RecordSetObj = database.Request('SELECT * FROM Info WHERE Value < 100');
IR.Log(RecordSetObj); // [object RecordSetPrototype]

RecordSet has its own properties and object-specific methods.

RecordSet object methods​

RecordSet contains records selected from an SQL database by a query. Use its methods to work with rows and columns or query its properties.

.GetRows()​

Returns the number of rows in RecordSet

Syntax

RecordSetObj.GetRows();

InputValueDescription
RecordSetObjtype: object
RecordSet object containing records selected by a database query
Output
rows1type: number
number of rows in the result set

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db');
mybase.Execute("BEGIN");
mybase.Execute('CREATE TABLE Info(ID INT, Value REAL, Name TEXT)');
mybase.Execute('INSERT INTO Info(ID, Value, Name) VALUES(1,19.9,"sunny day")');
var RecordSetObj = mybase.Request('SELECT * FROM Info WHERE ID<30');
IR.Log(RecordSetObj.GetRows());
mybase.Execute("COMMIT");
mybase.Close();
});

.GetColumns()​

Returns the number of columns in RecordSet

Syntax

RecordSetObj.GetColumns();

InputValueDescription
RecordSetObjtype: object
RecordSet object containing records selected by a database query
Output
columns1type: number
number of columns in the result set

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db');
mybase.Execute("BEGIN");
mybase.Execute('CREATE TABLE Info(ID INT, Value REAL, Name TEXT)');
mybase.Execute('INSERT INTO Info(ID, Value, Name) VALUES(1,19.9,"sunny day")');
var RecordSetObj = mybase.Request('SELECT * FROM Info WHERE ID<30');
IR.Log(RecordSetObj.GetColumns());
mybase.Execute("COMMIT");
mybase.Close();
});

.GetColumnName()​

Column name (column index: 0...n)

Syntax

RecordSetObj.GetColumnName(index);

InputValueDescription
RecordSetObjtype: object
RecordSet object containing records selected by a database query
index0type: number
column index, 0...n
Output
column nameIDtype: string
column name

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db');
mybase.Execute("BEGIN");
mybase.Execute('CREATE TABLE Info(ID INT, Value REAL, Name TEXT)');
mybase.Execute('INSERT INTO Info(ID, Value, Name) VALUES(1,19.9,"sunny day")');
var RecordSetObj = mybase.Request('SELECT * FROM Info WHERE ID<30');
IR.Log(RecordSetObj.GetColumnName(0)+ '\t'+ RecordSetObj.GetColumnName(1)+ '\t'+ RecordSetObj.GetColumnName(2));
mybase.Execute("COMMIT");
mybase.Close();
});

.GetColumnIndex()​

Returns a column's index by name

Syntax

RecordSetObj.GetColumnIndex(name);

InputValueDescription
RecordSetObjtype: object
RecordSet object containing records selected by a database query
name'id'type: string
column name
Output
index0type: number
column index (numbering starts at 0)

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db');
mybase.Execute("BEGIN");
mybase.Execute('CREATE TABLE Info(ID INT, Value REAL, Name TEXT)');
mybase.Execute('INSERT INTO Info(ID, Value, Name) VALUES(1,19.9,"sunny day")');
var RecordSetObj = mybase.Request('SELECT * FROM Info WHERE ID<30');
IR.Log(RecordSetObj.GetColumnIndex('Value')); // 1
mybase.Execute("COMMIT");
mybase.Close();
});

.GetColumnType()​

Column type: int (1), float (2), string (3). Column index: 0...n

Syntax

RecordSetObj.GetColumnType(index);

InputValueDescription
RecordSetObjtype: object
RecordSet object containing records selected by a database query
index0type: number
column index, 0...n
Output
type1type: number
data type:* 1 - int* 2 - float* 3 - string

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db');
mybase.Execute("BEGIN");
mybase.Execute('CREATE TABLE Info(ID INT, Value REAL, Name TEXT)');
mybase.Execute('INSERT INTO Info(ID, Value, Name) VALUES(1,19.9,"sunny day")');
var RecordSetObj = mybase.Request('SELECT * FROM Info WHERE ID<30');
IR.Log(RecordSetObj.GetColumnType(0)+ '\t'+ RecordSetObj.GetColumnType(1)+ '\t'+ RecordSetObj.GetColumnType(2));
mybase.Execute("COMMIT");
mybase.Close();
});

.GetRowValue()​

Table-cell value selected by column and row indexes (indexes: 0...n)

Syntax

RecordSetObj.GetRowValue(column, row);

InputValueDescription
RecordSetObjtype: object
RecordSet object containing records selected by a database query
column0type: number
column index, 0...n
row0type: number
row index, 0...n
Output
data1type: number, string
cell value

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db');
mybase.Execute("BEGIN");
mybase.Execute('CREATE TABLE Info(ID INT, Value REAL, Name TEXT)');
mybase.Execute('INSERT INTO Info(ID, Value, Name) VALUES(1,19.9,"sunny day")');
var RecordSetObj = mybase.Request('SELECT * FROM Info WHERE ID<30');
IR.Log(RecordSetObj.GetRowValue(0,0)+ '\t'+ RecordSetObj.GetRowValue(1,0)+ '\t'+ RecordSetObj.GetRowValue(2,0));
mybase.Execute("COMMIT");
mybase.Close();
});

.Free()​

Frees the RecordSet selection from memory to optimize memory use.

Syntax

RecordSetObj.Free();

InputValueDescription
RecordSetObjtype: object
RecordSet object containing records selected by a database query
Output
-

Example

var mybase = new SQL();
IR.AddListener(IR.EVENT_START,0,function()
{
mybase.Open('MyBase.db');
mybase.Execute("BEGIN");
mybase.Execute('CREATE TABLE Info(ID INT, Value REAL, Name TEXT)');
mybase.Execute('INSERT INTO Info(ID, Value, Name) VALUES(1,19.9,"sunny day")');
var RecordSetObj = mybase.Request('SELECT * FROM Info WHERE ID<30');
mybase.Execute("COMMIT");
mybase.Close();
RecordSetObj.Free();
});

RecordSet object properties​

RecordSetObj.property

PropertyValueDescription
Rows4type: Number
number of rows in the table
Columns12type: Number
number of columns in the table