Import & Export Database

A database that cannot leave is a trap. EXPORT DB writes one out as a file in your namespace; IMPORT DB reads one back in. Both work on a whole database or a single table, in the formats every other tool already speaks.

Exporting

EXPORT DB takes the database name, the format, and where the file should land. It returns the path it wrote, so the file is immediately yours to share, download or move.

A whole database, as an SQL dump
EXPORT DB "shopdb" AS "sql" INTO "/root/backups/shop.sql" SET ?path
AFTER EMIT ?path

The path is an ordinary namespace path, so everything you already do with files applies. Share the dump and you have a download link:

Export, then share it
EXPORT DB "shopdb" AS "sql" INTO "/root/shop.sql" SET ?path
AFTER FILE SHARE ?path SET ?url
AFTER EMIT ?url

One table

Add TABLE and only that table is written. CSV and TSV are single-table formats and require it — a spreadsheet has no way to hold several tables at once.

A single table, as CSV
EXPORT DB "shopdb" TABLE "people" AS "csv" INTO "/root/people.csv" SET ?path
AFTER EMIT ?path

The formats

FormatWhat you getWhole database?
"sql"A dump of CREATE TABLE and INSERT statements that opens in any database toolYes
"json"The same shape SELECT ROWS returns, with the schema alongsideYes
"csv"Comma-separated, a header row by defaultOne table only
"tsv"Tab-separated, otherwise identical to CSVOne table only
"xlsx"An Excel workbook, one sheet per tableYes

Structure or data

By default an export carries both the schema and the rows. STRUCTURE ONLY writes the shape and no data — useful for standing up an empty copy elsewhere. DATA ONLY writes the rows into a schema that already exists.

The shape, without the data
EXPORT DB "shopdb" AS "sql" INTO "/root/schema.sql" STRUCTURE ONLY SET ?path
AFTER EMIT ?path

Importing

IMPORT DB reads a file already in your namespace and writes what it finds into the named database. It returns what it did, so nothing has to be assumed.

Read a dump back in
IMPORT DB "shopdb" FROM "/root/backups/shop.sql" SET ?result
AFTER EMIT ?result("tables") & " tables, " & ?result("rows") & " rows"

A CSV goes into one named table. The header row names the columns, and a column the table does not have is skipped rather than failing the whole import.

A spreadsheet into a table
IMPORT DB "shopdb" TABLE "people" FROM "/root/people.csv" SET ?result
AFTER EMIT ?result("rows") & " rows added"

A workbook comes back the same way. Every sheet is a table, so a whole database exported to Excel imports as a whole database - name a table and only that one sheet is read.

A workbook back in, every sheet
IMPORT DB "shopdb" FROM "/root/shop.xlsx" SET ?result
AFTER EMIT ?result("tables") & " tables, " & ?result("rows") & " rows"
One sheet into one table
IMPORT DB "shopdb" TABLE "people" FROM "/root/people.xlsx" SET ?result
AFTER EMIT ?result

JSON carries the schema alongside the rows, so it is the format that can rebuild a table from nothing.

JSON, schema and all
IMPORT DB "shopdb" FROM "/root/shop.json" SET ?result
AFTER EMIT ?result

Tab-separated files read exactly like comma-separated ones - the extension decides which.

TSV, in and out
EXPORT DB "shopdb" TABLE "people" AS "tsv" INTO "/root/people.tsv" SET ?out
AFTER IMPORT DB "shopdb" TABLE "people" FROM "/root/people.tsv" REPLACE SET ?in
AFTER EMIT ?in("rows") & " rows came back"

The formats, both directions

FormatExportImportCarries the schema?
"sql"Whole database or one tableReplays every INSERT it findsYes, as CREATE TABLE
"json"Whole database or one tableWhole database or one tableYes - the only format that can rebuild a table with CREATING
"csv"One tableOne tableNo, the header names columns
"tsv"One tableOne tableNo
"xlsx"A sheet per tableEvery sheet, or one named tableNo, the first row names columns
An import reads the file, not the extension alone - a workbook is recognised as a workbook whatever it is called. Only CSV and TSV need TABLE, because a flat file has no way to say which table it belongs to.

What happens to what is already there

An import adds to a table by default. REPLACE empties it first, so what lands is exactly what was in the file. CREATING makes the database or table if it is not there yet, rather than refusing.

Replace the contents
IMPORT DB "shopdb" TABLE "people" FROM "/root/people.csv" REPLACE SET ?result
AFTER EMIT ?result
Into a database that does not exist yet
IMPORT DB "freshdb" FROM "/root/backups/shop.sql" CREATING SET ?result
AFTER EMIT ?result

A file with no header row

A CSV without a header is read positionally against the table's columns, in the order DESCRIBE TABLE reports them. The same modifier on an export leaves the header out.

NO HEADERS, both directions
IMPORT DB "shopdb" TABLE "people" FROM "/root/raw.csv" NO HEADERS SET ?in
AFTER EXPORT DB "shopdb" TABLE "people" AS "csv" INTO "/root/out.csv" NO HEADERS SET ?out
AFTER EMIT ?out

Headings and exact values

An import loses nothing. Each column's heading in the file is kept as the column's heading, word for word, and every value is stored exactly as the file holds it.

A workbook into a new table
IMPORT DB "shopdb" TABLE "staff" FROM "/root/staff.xlsx" CREATING SET ?result
AFTER EMIT ?result("columns")
(* {"date_of_birth": "Date of Birth", "employee_no": "Employee No.", ...} *)

ALL TEXT stores every value as text, exactly as written, whatever it looks like. HEADER ROWS n reads the first n rows together as the headings, for files whose headings span two rows.

Every value as text, two heading rows
IMPORT DB "shopdb" TABLE "ledger" FROM "/root/ledger.csv" CREATING ALL TEXT HEADER ROWS 2 SET ?result
AFTER EMIT ?result("rows")

What comes back

EXPORT DB returns the namespace path it wrote - a plain string. IMPORT DB returns an object:

KeyWhat it holds
okWhether the import finished
tablesHow many tables were written
rowsHow many rows landed
skippedRows the schema refused - a missing required column, a duplicate on a unique one
columnsEach column the file landed in, by name, with its heading
adjustedEvery change made to fit the file, such as a heading given a column name

Reference

SyntaxWhat it does
EXPORT DB "db" AS "fmt" INTO "path"Writes the whole database out
EXPORT DB "db" TABLE "t" AS "fmt" INTO "path"One table only
STRUCTURE ONLY / DATA ONLYSchema without rows, or rows without schema
IMPORT DB "db" FROM "path"Reads a whole database in
IMPORT DB "db" TABLE "t" FROM "path"Into one named table
REPLACEEmpty the table before writing
CREATINGMake the database or table if it is absent
NO HEADERSCSV and TSV without a header row; on import the columns are read positionally, in the order DESCRIBE TABLE reports them
Formats - "sql", "json", "csv", "tsv", "xlsx", each of which both exports and imports

What's next

Back to Databases & Tables for schemas, or Columns & Rows for working with data directly.