Commands

sql

Supported By

Syntax

sql connect to url [with driver driver] [with username username] [with password password]

sql disconnect [from url]

sql prepare [statement] query [using connection url]

sql prepare set parameter to value [using connection url]

sql prepare execute [using connection url]

sql execute prepared [using connection url]

sql execute query [using connection url]

URL yields the database's URL as a string. Driver yields the JDBC driver's class name as a string. Username and password yield the login name and password used to connect to the database as strings. Query yields a SQL query as a string. Parameter yields the index into the prepared statement's parameters, starting from 1, as an integer. Value yields the parameter's value as a string.

Examples

sql connect to "mysql://localhost/studentdb" with driver "com.mysql.jdbc.Driver" with username "admin" with password "woot"
sql execute "select * from students" using connection "mysql://localhost/studentdb"
sql prepare "update students set name=? where id=?" using connection "mysql://localhost/studentdb"
sql prepare set 1 to "Little Bobby Tables"
sql prepare set 2 to 16777216
sql execute prepared
sql disconnect from "mysql://localhost/studentdb"

Description

The sql command connects to, disconnects from, and submits queries to a database.

The sql connect form opens a connection to a database. If a connection has already been opened, the driver cannot be found or loaded, the database url is invalid, or the specified username and password are invalid, a script error is triggered.

The sql disconnect form closes an open connection to a database. If no url is specified, the last successfully-used url is used. If there is no open connection to the database, a script error is triggered.

The sql prepare statement form creates a prepared statement for the specified database. Parameters in the prepared statement are indicated by question marks in the query string. If no url is specified, the last successfully-used url is used. If there is no open connection to the database, a script error is triggered.

The sql prepare set form sets the value of a parameter in the last prepared statement created for the specified database. The first parameter in the prepared statement is parameter 1. If no url is specified, the last successfully-used url is used. If there is no open connection to the database, no prepared statement has been created for the specified database, or the specified parameter index is invalid, a script error is triggered.

The sql prepare execute and sql execute prepared forms execute the prepared statement. The number of rows affected or returned by the query is put into the result. If any rows are returned, they are put into it. Rows are delimited by the rowDelimiter and columns are delimited by the columnDelimiter. If no url is specified, the last successfully-used url is used. If there is no open connection to the database, the query contains a syntax error, or the query cannot be executed successfully, a script error is triggered.

The sql execute form executes a query immediately, without requiring the creation of a prepared statement. The number of rows affected or returned by the query is put into the result. If any rows are returned, they are put into it. Rows are delimited by the rowDelimiter and columns are delimited by the columnDelimiter. If no url is specified, the last successfully-used url is used. If there is no open connection to the database, the query contains a syntax error, or the query cannot be executed successfully, a script error is triggered.

Scripts

The following scripts demonstrate two possible ways to list the contents of a table:

sql execute "select * from students"
repeat foreach x in the rows of it
  put replaceall(x, the columndelimiter, tab)
end repeat
set the rowDelimiter to newline
set the columnDelimiter to tab
sql execute "select * from students"
put it

Notes

It is also possible to execute a query without quotes by leaving out the execute keyword, e.g.:

sql select * from students where firstname = "Steve"

However, this is prone to error and is not recommended. (If you're curious, among the reasons are: SQL and XION use different lexicographical rules, so it's possible for the query to confuse OpenXION or the database or both; it is impossible to use any XION expressions in such a statement, so you are limited to static queries; and this feature may change in future versions of OpenXION.)