Reference: Copy Statement
Copy statements allow bulk im- and export of tables:
Usage example:
-- Export
copy (select * from movies) to 'movies_backup.csv';
-- Import
copy movies2 from 'movies_backup.csv';Options
Copy supports a number of options controlling the format.
Format:csv,text,binary. Only for copy to:numpy.Delimiter: The record separator fortextandcsvformats. By default\t, and,.Header: If thetextandcsvhas a header. By defaultfalse.Null: The string that represents anullvalue intextandcsvformats. By default\N, and empty string.Quote: The quoting character for thecsvformat. By default". This can be used to insert values containing the delimiter.Escape: The escape character for thecsvformat. By default". This can be used to escape the quote character.Force Not Null: Do not match the specified columns’ values against the null string. In the default case where the null string is empty, this means that empty values will be read as zero-length strings rather than nulls, even when they are not quoted. If*is specified, the option will be applied to all columns. This option is allowed only inCOPY FROM, and only when usingcsvformat.Force Null: Match the specified columns’ values against the null string, even if it has been quoted, and if a match is found set the value toNULL. In the default case where the null string is empty, this converts a quoted empty string into NULL. If*is specified, the option will be applied to all columns. This option is allowed only inCOPY FROM, and only when usingcsvformat.ℹ️Note:FORCE_NULLandFORCE_NOT_NULLcan be used simultaneously on the same column. This results in converting quoted null strings to null values and unquoted null strings to empty strings.
CedarDB also supports a best-effort import mode:
(on_error ignore): By default, Copy aborts when encountering invalid rows. With this option, rows containing mismatching elements will be skipped on a best-effort basis.
A query that uses all available copy options could look like this:
COPY target_table (column1, column2, column3)
FROM '/absolute/path/to/your_file.csv'
WITH (
FORMAT csv,
DELIMITER ',',
HEADER true,
NULL 'null',
QUOTE '"',
ESCAPE '\',
FORCE_NOT_NULL (column1, column2),
FORCE_NULL (column3),
ON_ERROR ignore
);