Scripting Vs. SQL Vs. Expressions
Expression Language & SQL Queries vs Scripting
There are three major languages in Ignition, the Expression language, the SQL Queries, and Python Scripting. It is important to understand the differences between the three and to know where each is used. Scripting is used in the event handlers that are available all over Ignition, but Expressions and SQL are in the Property Binding locations shown here.
Spot the Difference - Comments
When starting out with Ignition, it can be difficult to know which syntax you should be using for a particular area of text. One little trick that may help is to be familiar with how each language handles comments, and then utilize the Ctrl + / command which automatically adds the characters that comments out a line of code in the language are you are typing in. Once you remember to use Ctrl + /, you simply need to be familiar with the characters that each language uses.
// The Expression Language uses double forward slashes.
-- Areas in Ignition that accept SQL syntax use double dashes.
# Python uses the pound/number/hash character.
In addition to comments, interfaces for each of the languages usually contain other signs or reminders about the language. These will be covered in their respective sections on this page.
Python Scripts
Overview
Python is featured prominently throughout Ignition and many different resources can contain a Python Script.
How Can I Tell If I'm Writing a Python Script?
Python Scripts typically use the words Script or Event in the interface. Additionally, Python requires a particular Event to be selected so if you see event Handlers on the left, you know you are looking at a Python script.
Where Are Python Scripts Used?
Python Scripts are used all throughout Ignition. Some resources, such as components, even have multiple places to write scripts! Below are some common locations for scripts:
SQL Queries
Overview
The SQL language is used for selecting information from a database. It can be used in a variety of ways, but most of them will either make modifications to the database or return a set of values (with a few notable exceptions like Stored Procedures). The majority of users will be returning large chunks of data into a Table or Report in Ignition. This means a complete dataset will be returned based on a user selection, a time range, or any combination of factors.
How Can I Tell If I Should Be Using SQL Syntax?
Typically, the words Query and Database appear in the interface somewhere. Additionally, there is usually a way to specify a Database Connection.
Where Is SQL Used in Ignition?
Below is a reference of the most common areas in Ignition where SQL queries may be used.
- SQL Query Bindings
- Named Queries
- Database Query Browser
- Reporting Data Source:
- Query Tags
- Python Scripts - See the SQL in Python header below for more details
SQL in Python
SQL queries can be called from a Python script. There are several system functions in Ignition that allow a script to run a query against the database, such as system.db.runPrepQuery. This is a more advanced technique, as you need to adhere to both language's syntax. Furthermore, when typing a SQL query in a Python script interface, the syntax highlighting can not help with the SQL portions. The syntax highlighting in a Scripting Window is only looking for Python syntax, not SQL.
In cases where you plan on calling a SQL query from a Python script, it is highly recommended to write the query in the Database Query Browser first (substituting parameters with static values for testing purposes), and then move the query over to the script once the query executes successfully on its own. This approach can save you some time troubleshooting, as there will be less ambiguity when an error occurs since you know the query runs.
Below we see an example of calling a SQL query in a script. Line 5 creates a variable called "query", and assigns it a string consisting of a prepared statement (using SQL). The query is then executed with the system.db.runPrepUpdate function.
For more examples of using a query in a Python Script, check out the system.db.* functions.
Expression Language
Overview
The Expression Language is a simple programming language that we invented (very similar to many other existing expression languages), and is different from the Python scripting you will find in Ignition. The expression language is a very simple kind of language where everything is an expression - which is a single piece of code that returns a value. This means that there are no statements, and no variables, just operators, literals, and functions.
The most common expression language that most people are familiar with is the one found in Microsoft Excel. You can have Excel calculated a cell's value dynamically by typing an expression like =SUM(C5:C10)
. Our expression language has similar functionality, but different syntax. It is mainly used to define dynamic values for Tags and component properties. In Ignition's Expression Language, you will use component properties and functions like if({Root Container.type}="Type C",True,False).
How Can I Tell If I'm Writing an Expression?
Typically interfaces that expect the Expression Language use the word Expression. Additionally, you'll commonly find the Expression Function button towards the right side of the text area.
Where Is The Expression Language Used?
Below is a list of resources that commonly utilize the Expression Language
- Expression Bindings on Property Bindings and Alarm Bindings
- Expression Tags and Derived Tags
- Several Alarm Pipeline Blocks, including the Expression Block, Switch Block, and Notification Blocks.
- Expression Items in Transaction Groups
- Parameters on Reports
- SFC Transitions