Importing Data
Kvery can feed the rows of an uploaded file into a query, letting you insert or update many records from a spreadsheet or data export without writing a script.
How importing works
An import connects two queries:
- a read query (one with
SELECTorEXEC) whose result table gets an Import button, and - an import query that handles a single row. It must be a write query (its SQL
contains
INSERT,UPDATE,DELETE,REPLACE,ALTERorCREATEfollowed by a space): only a write query receives the values of the rows.
Choose the import query in the read query's Import settings (see Query settings). On the run page of the read query, Import above the table opens a file picker. Kvery reads the file and runs the import query once for each row, passing the row's values in as parameters. This is built on the same form variables mechanism — the columns in your file map to the variables of the import query.
For example, this import query inserts one contact for each row:
INSERT INTO contacts (name, email, country)
VALUES ('$name', '$email', '$country');
Each row of the uploaded file supplies $name, $email, and $country.
The file
- Format — a CSV, XLS or XLSX file, with the extension
.csv,.xlsor.xlsxin lower case. Kvery recognises the type of the file from its content, so an HTML table saved as.xlsis read as well. Of a workbook, the active sheet is read. - Column names — the first row holds the column names. Each name becomes a
parameter name: accents are removed, every run of characters other than letters
and digits becomes
_, and the name is put in lower case — so the columnE-mail addressfills$e_mail_address. A column without a name in the first row is left out. - Renaming columns — the Query parameters setting of the import maps a
column to another parameter name with a
column=parameterline (for exampleCountry code=country). A line whose value contains$name, such aslist_id=$list_id, passes the value oflist_idfrom the address of the page to every row instead. - Empty rows are skipped, wherever they are in the file. A row counts as empty
when it has no value, and also when its only value is a single
0. - Only the cell values are read: formulas are calculated, a value arrives as the sheet displays it (with its number or date format) and without the spaces around it. Pictures in the file are ignored (also the images of an HTML table).
- Files that are not imported — a file with another extension, a file Kvery cannot read and an XLSX file with a picture linked to an address outside the file: the page shows "Something went wrong!" and no row is imported.
- Kvery's query syntax — a cell value that the import query uses and that
contains Kvery's own query syntax (for example
*/5 * * * *or[50%], see How values are inserted) is refused with "A parameter value is not allowed: it contains syntax of the query engine (…)", and the import stops at that row.
After the first rows were imported, the editor of the import query lists the parameter names it received ("Import parameters (double-click to insert to editor):"); double-click a name to insert it into the SQL, or close the list with its ×.
When every row has run, the table of the read query reloads.
Row-by-row processing
Imports are processed row by row so that each record is handled predictably and errors can be reported against the specific row that failed. While the import runs, the page shows "Processing ..." with the number of rows done. The import stops at the first row that fails: the rows before it are already imported, and the page shows "Row:" (or "Batch:") with its number and the error, so you can correct your file and import the remaining rows. The number after "Row:" counts the header as row 1 and then the rows that are not empty.
The page waits at most 60 seconds for one batch. A batch that takes longer is reported as failed with "Something went wrong!", although the server may still finish it, and the rows after it are not sent: lower the batch size for slow import queries.
Importing on a shared page
The Import button also works on the shared link of the read query and on a shared dashboard. A visitor who cannot run the import query as a member of its group can import only when the import query is shared as well (and, when it has a share password, after that password was entered); otherwise the page shows "You do not have sufficient privileges!". On these pages the import query runs its published version, in the app its draft.
Import batch size
Each query has an import batch size: the number of rows of the file processed in one request. It defaults to 1, but you can change it on the query's import settings (Processing at the same time) — so this is a configurable default, not a fixed platform limit. With a batch size of 1 an import error names the row that failed; with a larger batch size it names the batch. See Plans and limits.
Tips for reliable imports
- Make sure your file's columns line up with the query's form variables.
- Use a draft to test the import query on a small file first.
- Validate or clean the data in your file before importing — Kvery runs exactly the SQL you wrote against each row.
- Watch the logs during a large import to catch failures early.
Related
- Form variables — how row values become query parameters.
- Running a query — forms and how the result is loaded.