- Reference
- Databases
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();
| Input | Value | Description |
|---|---|---|
| - |
| Output | ||
|---|---|---|
| Object | [object SQLPrototype] | type: Object object representing the system database |
Example
var systembase = IR.GetDatabase();
IR.Log(systembase) // [object SQLPrototype]
Notes
- 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();
| Input | Value | Description |
|---|---|---|
| - |
| Output | ||
|---|---|---|
| Object | [object SQLPrototype] | type: Object object representing a user database |
Example
var mybase = new SQL();
IR.Log(mybase); // [object SQLPrototype]
Notes
- 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);
| Input | Value | Description |
|---|---|---|
| sqlobject | type: object object representing a user or system database | |
| undeleted | true | type: 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
- 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);
| Input | Value | Description |
|---|---|---|
| sqlobject | type: 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
- 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);
| Input | Value | Description |
|---|---|---|
| sqlobject | type: 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
- The query returns a RecordSet object containing the result
.Close()
Finish working with the database
Syntax
sqlobject.Close();
| Input | Value | Description |
|---|---|---|
| sqlobject | type: 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
- 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();
| Input | Value | Description |
|---|---|---|
| RecordSetObj | type: object RecordSet object containing records selected by a database query |
| Output | ||
|---|---|---|
| rows | 1 | type: 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();
| Input | Value | Description |
|---|---|---|
| RecordSetObj | type: object RecordSet object containing records selected by a database query |
| Output | ||
|---|---|---|
| columns | 1 | type: 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);
| Input | Value | Description |
|---|---|---|
| RecordSetObj | type: object RecordSet object containing records selected by a database query | |
| index | 0 | type: number column index, 0...n |
| Output | ||
|---|---|---|
| column name | ID | type: 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);
| Input | Value | Description |
|---|---|---|
| RecordSetObj | type: object RecordSet object containing records selected by a database query | |
| name | 'id' | type: string column name |
| Output | ||
|---|---|---|
| index | 0 | type: 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);
| Input | Value | Description |
|---|---|---|
| RecordSetObj | type: object RecordSet object containing records selected by a database query | |
| index | 0 | type: number column index, 0...n |
| Output | ||
|---|---|---|
| type | 1 | type: 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);
| Input | Value | Description |
|---|---|---|
| RecordSetObj | type: object RecordSet object containing records selected by a database query | |
| column | 0 | type: number column index, 0...n |
| row | 0 | type: number row index, 0...n |
| Output | ||
|---|---|---|
| data | 1 | type: 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();
| Input | Value | Description |
|---|---|---|
| RecordSetObj | type: 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
| Property | Value | Description |
|---|---|---|
| Rows | 4 | type: Number number of rows in the table |
| Columns | 12 | type: Number number of columns in the table |