Database Manual · Chapter 2

Connection Settings

The DB_Sqlite sample runs on a single SQLite connection named local. Connection information is stored in XDatabase\connections.json in the project folder and edited on the Database → Database Connections screen of the Solution Explorer.

When a project opens, this file is read and the connections are registered before the scripts are built. That is why scripts (Data.xms etc.) can reach the connection with a single line: DB["local"]. Registration does not open the connection; scripts open it by calling Open().

Note The project file (.xmp) also has a DatabaseOption block, but it is a copy written when the project is saved. connections.json is what is used when the project opens, so change connections on the Database Connections screen or in connections.json.

Database Connections Screen

Database Connections — DB_Sqlite sample
Database Connections — DB_Sqlite sample
ItemDescription
Connection cardShows name · type (sqlite / mssql) · path or server · description. The dot on the left is green while the connection is open and gray when closed
Card context menuOpen · Close · Test Connection · Remove. The test here uses the values applied to the card
Selected ConnectionDetails of the selected connection — Name · Type and, for sqlite, Path; for mssql, Server · Database · User · Password · Conn Timeout (s) · Cmd Timeout (s) · Ping Interval (s) · Reconnect; and Description
Apply · Test ConnectionApplies the entered details to the card / tests the connection with the values being entered
Backup Folder · Auto Backup · hours · Keep LastBackup folder · automatic backup on/off and interval in hours · number to keep. In the current version these values are only saved; automatic backup does not run (Chapter 9)
Journal ModeSQLite journal mode : WAL (default) or DELETE
Add · RemoveCreates a new SQLite connection (db1, db2 …) / removes the selected connection from the list after confirmation
Open Database StudioSaves the current contents and opens Database Studio
SAVESaves all settings and registers the DB connections again (connections that were open are closed). Details not yet applied with [Apply] are included, and if a SQLite path was changed, you are asked what to do with the existing DB file (Move · Copy · Leave · Cancel)
RELOADReads the file again after confirmation (unsaved changes are discarded)

The Sample's Actual Settings

This is XDatabase\connections.json of DB_Sqlite.

json
{
  "Connections": [
    {
      "Name": "local",
      "Type": "sqlite",
      "Path": "XDatabase/LocalDB.db",
      "Server": "",
      "Database": "",
      "User": "",
      "PasswordEnc": "",
      "ConnectionTimeout": 15,
      "CommandTimeout": 30,
      "PingIntervalSec": 10,
      "ReconnectRetries": 3,
      "Description": "테스트용 db"
    }
  ],
  "BackupFolder": "XDatabase/",
  "AutoBackupEnabled": false,
  "AutoBackupIntervalHours": 24,
  "BackupKeepLast": 30,
  "JournalMode": "WAL"
}

Field Reference

Connection (an item of the Connections array)

KeyValue / Meaning
Name"local" — the name scripts use as DB["local"]
Type"sqlite" or "mssql"
PathSQLite file path. A relative path is based on the project folder; if empty, XDatabase\ConnectionName.db is used
Server · Database · UserMSSQL only. If User is empty, Windows integrated security is used
PasswordEncThe encrypted MSSQL password. Enter the password in the Password box on the screen
ConnectionTimeoutMSSQL connection timeout (seconds). Default 15
CommandTimeout · PingIntervalSec · ReconnectRetriesMSSQL command timeout · disconnection check interval · number of automatic reconnects. In the current version they are only saved and not applied to the connection (always 30 s · 10 s · 3 times)
DescriptionDescription. Shown on the card; no effect on behavior

A SQLite connection uses only Name · Type · Path; the timeout and reconnect values apply to MSSQL only.

Caution Passwords are encrypted with Windows DPAPI (current user). When the project is moved to another PC or Windows account, decryption fails and the password becomes empty, so enter the password again on the deployed machine and save.

Options (outside the Connections array)

KeyMeaning
BackupFolderBackup folder (default XDatabase/Backup; the sample uses XDatabase/). Only saved in the current version
AutoBackupEnabled · AutoBackupIntervalHours · BackupKeepLastAutomatic backup on/off · interval (hours, default 24) · number to keep (default 30). In the current version the values are only saved; automatic backup does not run
JournalModeSQLite journal mode. With "WAL" (default) the connection is set to WAL mode when it opens. The other choice is "DELETE" (Chapter 9)

Run backups yourself with [Backup...] in Database Studio or with Backup() · StartBackupThread() in scripts (Chapter 9).

SQLite and MSSQL

ItemSQLiteMSSQL
Type"sqlite""mssql"
Required valuesPath (if empty, XDatabase\ConnectionName.db)Server · Database (and Password when User is set)
Parameter placeholder?? (converted to @p0, @p1 … internally)
Backup (Backup · StartBackupThread)SupportedNot supported — fails, and OnBackupDone is called with failure
Disconnection detection—The connection is checked periodically; OnRemoteDbDisconnected when lost and OnRemoteDbReconnected after automatic reconnection

The same script code (DB[...].RunSqlSelect(...) and so on) works on both databases. The SQL statements themselves follow each database's syntax.

Two Ways to Register a New Connection

xscript
Solution Explorer
├── Data Editor Pages
└── Database                       ← top-level group, peer of Data Editor Pages
    ├── Database Connections       ← this chapter
    ├── Database Studio            (Chapters 3 · 4)
    └── Database Events            (Chapter 8)
  1. Press [Add] to create a new SQLite connection card with a name such as db1.
  2. In Selected Connection on the right, enter Name · Type · Path (for MSSQL, Server · Database · User · Password) · Description and press [Apply].
  3. Test the connection with [Test Connection].
  4. Press [SAVE]; connections.json is saved and the connections are registered again, so scripts can use DB["name"].

B. Editing connections.json Directly

You can also open XDatabase\connections.json in a text editor and add an object to the Connections array. The change takes effect after you reopen the project, or after you read the file again with RELOAD on the Database Connections screen and press [SAVE]. The MSSQL password is stored encrypted, so it cannot be written into the file directly; enter it in the Password box on the screen.

Next Chapter

Once the connection is in place, the next step is creating the table. The sample uses a single table, order_history, for every demonstration.

Subscribe