- Reference
- ODBC
Working with databases via ODBC
iRidi Server provides script-controlled functionality for local or remote access through ODBC (Open Database Connectivity) to databases such as MySQL, PostgreSQL, SQLite, Oracle, SQL Server, Microsoft Access, dBase, Paradox, FoxPro, and others.
ODBC is a paid iRidi Server feature, but you can try it in test mode. Available in iRidi Pro 1.2.10 and later.
The ODBC driver works only in iRidi Server running on Windows (x86/x64). ODBC functionality and support for some DBMSs (SQL Server, Microsoft Access, dBase, Paradox, FoxPro) are included by default in the standard Windows installation package.
For other databases, download the Windows ODBC connection software from the corresponding DBMS vendor. For example, for MySQL, download Connector/ODBC for Windows: https://dev.mysql.com/downloads/connector/odbc/
(The ODBC connection software architecture must match that of iRidi Server.)
- In Windows, find and run ODBC Data Sources (32-bit or 64-bit).

- Add a new database connection and configure it.
-865dcbd389b551826b970881e3c0a06d.png)
For security, it is not recommended to fill in the User and Password fields in ODBC Data Sources, because they are not encrypted there. iRidi Server passes these credentials when connecting to the database.
- Write a script that creates an ODBC object and queries the database using .Query().
For the license to activate correctly on the server, scripts must connect to ODBC after a short delay rather than on EVENT_START, for example using IR.SetTimeout() or a button press.
Checking the database connection
When creating an ODBC object, the script cannot identify the database connection and issues a query even if the connection has not been established. In this case, check the database connection first. For example, we need to connect to a database created in MySQL. To do this, create a project and a script in it.
IR.AddListener(IR.EVENT_START,0,function()
{
var BaseMySQL = new ODBC("root", "123", "MySQLresurs");
if(BaseMySQL == false)
{
IR.Log("Wrong connection");
}
else
{
IR.Log("Right connection");
}
});
Run the project:
- if MySQL connects, data is output to the log;
- if MySQL does not connect, false appears in the log. The same check must also be performed in a server project.
Example. Writing to a database
This script retrieves power consumption and temperature data at two points from an energy meter and temperature sensors over Modbus TCP, then writes the measured values to a local SQLite database through ODBC.
The same approach can be used to write data to any local or remote SQL database.
var SQLite = false;
var query_head = "INSERT INTO measure(datetime, consumption, temperature1, temperature2) VALUES(";
var cdt = 0;
var cons = 0;
var temp1 = 0;
var temp2 = 0;
IR.AddListener(IR.EVENT_START,0,function()
{
IR.SetInterval(1000, getData);
});
function getData()
{
if (!SQLite) //if no ODBC object
{
SQLite = new ODBC("", "", "sqlite3"); // creating ODBC object
if (SQLite)
{
IR.Log(SQLite + " connected!");
}
}
else
{
var currentdatetime = new iDate();
cdt = currentdatetime.value;
// current date and time in OLE Automation date format (OADate)
cons = IR.GetDevice("Modbus TCP").GetFeedback("Channel 1");
temp1 = IR.GetDevice("Modbus TCP").GetFeedback("Channel 2");
temp2 = IR.GetDevice("Modbus TCP").GetFeedback("Channel 3");
var query = query_head + cdt + "," + cons + "," + temp1 + "," + temp2 + ");";
/*(datetime, consumption, temperature1, temperature2)*/
var response = SQLite.Query(query); // SQL query to SQLite database
IR.Log(query);
}
}
Example. Reading from a database
This example queries a MySQL database, unpacks the response into an array, then displays the first element of the "User" field.
When reading from an SQL database, take memory and processor performance limits into account.
IR.AddListener(IR.EVENT_START,0,function()
{
IR.SetTimeout(50, getData);
// waiting 50 milliseconds for appropriate iRidium ODBC driver license activation
});
function getData()
{
var MySQL = new ODBC("root", "", "MySQLresurs"); // creating database connection object
var response = MySQL.Query("SELECT * FROM users"); // SQL query to MySQL database
if (response) // if connection is successful and answer exists
{
var rows = response.GetRows();
var columns = response.GetColumns();
var data = {}; // two-dimension array with query result
for (var i = 0; i < columns; i++) {
var colName = response.GetColumnName(i);
data[colName] = [];
for (var j = 0; j < rows; j++) {
data[colName][j] = response.GetRowValue(i, j);
}
}
response.Free(); // free memory of recordset (optional action for memory optimization)
IR.Log(data["User"][0]); // first user in MySQL table
}
}
ODBC object
new ODBC()
Creates an ODBC object that connects to the database and contains the single method Query().
Syntax
new ODBC(login, password, dsn);
| Input | Value | Description |
|---|---|---|
| login | 'myname' | type: string Login (username) for connecting to the database |
| password | 'qwerty' | type: string Password for connecting to the database (can be empty for a local database) |
| dsn | 'MySQLresurs' | type: string ODBC data source name (DSN) |
| Output | ||
|---|---|---|
| object | [object DevicePrototype] | type: object ODBC connection false if the connection fails (nonexistent connection or incorrect password) |
Example
var MySQL = new ODBC("root", "", "MySQLresurs");
IR.Log(MySQL); // [object DevicePrototype]
Store the object in a variable to access it
ODBC object methods
.Query()
Queries the database and returns the response as a RecordSet.
SQL query result sets can be large. When working with large result sets, the developer must consider system limits: RAM size, processor and communication controller performance, and network interface bandwidth. Programmatically limiting result set size is recommended.
Syntax
ODBCobj.Query(query);
| Input | Value | Description |
|---|---|---|
| ODBCobj | [object DevicePrototype] | type: object ODBC connection object |
| query | 'SELECT * FROM orders' | type: string Query string |
| Output | ||
|---|---|---|
| RecordSetObj | [ ] | type: object RecordSet result set object. If the query does not return a response, the returned value is false. |
Example
`var response = DB.Query("SELECT * FROM users");`
.ParameterizedQuery()
Queries the database and returns the response as a RecordSet result set. (The method supports Cyrillic characters)
SQL query result sets can be large. When working with large result sets, the developer must consider system limits: RAM size, processor and communication controller performance, and network interface bandwidth. Programmatically limiting result set size is recommended.
Syntax
ODBCobj.ParameterizedQuery(String, Array);
| Input | Value | Description |
|---|---|---|
| ODBCobj | [object DevicePrototype] | type: object ODBC connection object |
| String | "" | type: string where name is any unique name and char[200] is the data type for this column |
| Array | [ ] | type: type array of data to pass to the database: array |
| Output | ||
|---|---|---|
| RecordSetObj | [ ] | type: object RecordSet result set object. If the query does not return a response, false is returned. |
Example
var response = var SQLite = false;
IR.AddListener(IR.EVENT_START,0,function() {
IR.SetInterval(1000, getData);
});
function getData() {
if (!SQLite) { //if no ODBC object
SQLite = new ODBC("root", "", "Test"); // creating ODBC object
if (SQLite)
{
IR.Log(SQLite + " connected!");
};
};
};
function Intu () {
var query = "INSERT INTO TestODBC(Intu) VALUES(:name<int>);"
var arr = [654];
var response = SQLite.ParameterizedQuery(query, arr); // SQL query to SQLite database
};
Initial data: you have a database named TestODBC containing a row named Intu with the data type int.