Repository navigation
Database Connections
OpenRPG uses ODBC for database access, making it portable across databases. You connect using a connection string with EXEC SQL CONNECT USING.
macOS / Linux:
- Install unixODBC (the driver manager)
- Install a database-specific ODBC driver
- Register the driver in
/etc/odbcinst.inior (on macOS with Homebrew)/opt/homebrew/etc/odbcinst.ini
Windows:
- ODBC driver manager is built in — no additional install needed
- Install a database-specific ODBC driver (typically an MSI installer)
- Drivers are registered automatically; manage them via the ODBC Data Source Administrator (search "ODBC" in the Start menu)
SQLite is ideal for local development and testing — no server required.
Install:
# macOS
brew install sqliteodbc
# Linux (Debian/Ubuntu)
sudo apt install libsqliteodbcRegister driver (macOS — add to /opt/homebrew/etc/odbcinst.ini):
[SQLite3]
Description=SQLite3 ODBC driver
Driver=/opt/homebrew/lib/libsqlite3odbc.dylib
Threading=2Register driver (Linux — add to /etc/odbcinst.ini):
[SQLite3]
Description=SQLite3 ODBC driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libsqlite3odbc.so
Threading=2Connect:
DCL-S connStr VARCHAR(200);
connStr = 'Driver={SQLite3};Database=/path/to/mydb.sqlite;';
EXEC SQL CONNECT USING :connStr;Windows:
Download the SQLite ODBC driver from http://www.ch-werner.de/sqliteodbc/ and run the MSI installer. The driver is registered automatically. Use the same connection string as above.
The database file is created automatically if it doesn't exist.
Install:
# macOS
brew install psqlodbc
# Linux (Debian/Ubuntu)
sudo apt install odbc-postgresqlRegister driver (macOS — add to /opt/homebrew/etc/odbcinst.ini):
[PostgreSQL]
Description=PostgreSQL ODBC driver (ANSI)
Driver=/opt/homebrew/lib/psqlodbca.so
Threading=0
[PostgreSQL Unicode]
Description=PostgreSQL ODBC driver (Unicode)
Driver=/opt/homebrew/lib/psqlodbcw.so
Threading=0Register driver (Linux — add to /etc/odbcinst.ini):
[PostgreSQL]
Description=PostgreSQL ODBC driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/psqlodbca.so
Threading=0
[PostgreSQL Unicode]
Description=PostgreSQL ODBC driver (Unicode)
Driver=/usr/lib/x86_64-linux-gnu/odbc/psqlodbcw.so
Threading=0Windows:
Download the PostgreSQL ODBC driver from https://www.postgresql.org/ftp/odbc/versions/msi/ and run the MSI installer. The driver name is typically PostgreSQL Unicode(x64).
Connect:
DCL-S connStr VARCHAR(300);
// Basic connection
connStr = 'Driver={PostgreSQL};Server=localhost;Port=5432;'
+ 'Database=mydb;Uid=myuser;Pwd=mypassword;';
EXEC SQL CONNECT USING :connStr;
// Windows (use the registered driver name)
connStr = 'Driver={PostgreSQL Unicode(x64)};Server=localhost;Port=5432;'
+ 'Database=mydb;Uid=myuser;Pwd=mypassword;';
EXEC SQL CONNECT USING :connStr;Install:
# macOS
brew install mysql-connector-odbc
# Linux (Debian/Ubuntu)
sudo apt install libmyodbcRegister driver (macOS — find the .so path with find /opt/homebrew -name "libmyodbc*"):
[MySQL]
Description=MySQL ODBC driver
Driver=/opt/homebrew/lib/libmyodbc8a.so
Threading=0Windows:
Download MySQL Connector/ODBC from https://dev.mysql.com/downloads/connector/odbc/ and run the MSI installer. The driver name is typically MySQL ODBC 8.0 Unicode Driver.
Connect:
DCL-S connStr VARCHAR(300);
// macOS / Linux
connStr = 'Driver={MySQL};Server=localhost;Port=3306;'
+ 'Database=mydb;User=myuser;Password=mypassword;';
EXEC SQL CONNECT USING :connStr;
// Windows
connStr = 'Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Port=3306;'
+ 'Database=mydb;User=myuser;Password=mypassword;';
EXEC SQL CONNECT USING :connStr;Install:
# macOS
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew install msodbcsql18
# Linux (Debian/Ubuntu) — see Microsoft's docs for repo setup
sudo apt install msodbcsql18Windows:
Download "ODBC Driver 18 for SQL Server" from Microsoft's download page and run the MSI installer. SQL Server Express also installs the driver automatically.
Connect:
DCL-S connStr VARCHAR(300);
connStr = 'Driver={ODBC Driver 18 for SQL Server};'
+ 'Server=localhost,1433;Database=mydb;'
+ 'Uid=sa;Pwd=mypassword;TrustServerCertificate=yes;';
EXEC SQL CONNECT USING :connStr;The connection string is the same on all platforms.
Install (macOS / Linux):
# Download IBM Data Server Driver from IBM's website
# Or use the IBM i Access ODBC driver for IBM i connectionsInstall (Windows):
Download the IBM Data Server Driver Package from IBM Fix Central or install IBM i Access Client Solutions which includes the IBM i Access ODBC Driver. Run the installer and the driver is registered automatically.
Connect:
DCL-S connStr VARCHAR(300);
// Db2 on Linux/Windows
connStr = 'Driver={IBM DB2 ODBC DRIVER};'
+ 'Database=mydb;Hostname=localhost;Port=50000;'
+ 'Uid=db2admin;Pwd=mypassword;Protocol=TCPIP;';
EXEC SQL CONNECT USING :connStr;
// IBM i (via IBM i Access ODBC Driver)
connStr = 'Driver={IBM i Access ODBC Driver};'
+ 'System=myibmi.example.com;'
+ 'Uid=myuser;Pwd=mypassword;';
EXEC SQL CONNECT USING :connStr;Avoid hardcoding credentials. Use %GETENV to read connection details from the environment:
DCL-S connStr VARCHAR(300);
connStr = %GETENV('DATABASE_URL');
IF connStr = '';
DSPLY 'DATABASE_URL not set!';
RETURN;
ENDIF;
EXEC SQL CONNECT USING :connStr;export DATABASE_URL="Driver={PostgreSQL};Server=localhost;Port=5432;Database=mydb;Uid=user;Pwd=pass;"
./myprogrammacOS / Linux — list registered drivers:
odbcinst -q -dTest a connection with isql (comes with unixODBC):
isql MyDSN myuser mypasswordWindows — open the ODBC Data Source Administrator (search "ODBC" in the Start menu). The Drivers tab lists all registered drivers. Use the Test Connection button when configuring a data source.
// Connect
EXEC SQL CONNECT USING :connStr;
// ... do work ...
// Disconnect when done
EXEC SQL DISCONNECT;
// Or use CONNECT RESET (equivalent to DISCONNECT)
EXEC SQL CONNECT RESET;Note: OpenRPG supports one database connection per program. You must disconnect before connecting to a different database.
- Installation
- Quick Start
- CLI Reference
- Language Basics
- Data Types
- Variables and Constants
- Data Structures
- Arrays
- Varying-Dimension Arrays
- Control Flow
- Procedures
- Procedure Overloading
- Built-In Functions
- Error Handling
- Operation Extenders
- Enumerations
- Program Status Data Structure
- Data Areas
- SND-MSG
- Embedded SQL
- Record-Level Access
- Display Files (WORKSTN)
- Program-Described Files (I-Specs and O-Specs)
- DATA-INTO and DATA-GEN
- XML-INTO
- Fixed-Format Source
- Database Connections
- Multi-Module Programs
- Environment Variables
- Testing
- Complete Example: Employee Report
- IBM i Conformance Results
- IBM i Execution Results