SIMPLE_SQL_EXPORT

Export database tables to CSV, JSON, or SQL dump formats.

View Source

Creation

make (a_db: SIMPLE_SQL_DATABASE)
Create exporter attached to database.

CSV Export

to_csv (a_table: READABLE_STRING_8): STRING_32
Export table to CSV string.
to_csv_file (a_table: READABLE_STRING_8; a_path: READABLE_STRING_GENERAL)
Export table to CSV file.
query_to_csv (a_sql: READABLE_STRING_8): STRING_32
Export query results to CSV string.

JSON Export

to_json (a_table: READABLE_STRING_8): STRING_32
Export table to JSON array of objects.
to_json_file (a_table: READABLE_STRING_8; a_path: READABLE_STRING_GENERAL)
Export table to JSON file.
query_to_json (a_sql: READABLE_STRING_8): STRING_32
Export query results to JSON.

SQL Dump

to_sql (a_table: READABLE_STRING_8): STRING_32
Export table as CREATE TABLE + INSERT statements.
dump_schema: STRING_32
Export all CREATE TABLE statements.
dump_all: STRING_32
Full database dump (schema + data).
dump_to_file (a_path: READABLE_STRING_GENERAL)
Write full dump to file.

Examples

Export to CSV

local
    export: SIMPLE_SQL_EXPORT
    csv: STRING_32
do
    create export.make (db)

    -- Export entire table
    csv := export.to_csv ("users")
    -- Result: "id,name,email\n1,Alice,alice@example.com\n2,Bob,..."

    -- Or save directly to file
    export.to_csv_file ("users", "users_export.csv")
end

Export Query Results

-- Export filtered/transformed data
csv := export.query_to_csv ("SELECT name, email FROM users WHERE active = 1 ORDER BY name")

json := export.query_to_json ("SELECT id, title, created_at FROM posts WHERE published = 1")

Export to JSON

local
    export: SIMPLE_SQL_EXPORT
    json: STRING_32
do
    create export.make (db)
    json := export.to_json ("users")
    -- Result: [{"id":1,"name":"Alice","email":"alice@example.com"},...]

    export.to_json_file ("settings", "settings_backup.json")
end

Full Database Dump

local
    export: SIMPLE_SQL_EXPORT
do
    create export.make (db)

    -- Export schema only
    print (export.dump_schema)
    -- Result: CREATE TABLE users (...); CREATE TABLE posts (...); ...

    -- Full dump to file
    export.dump_to_file ("full_backup.sql")
end

Export Single Table to SQL

local
    export: SIMPLE_SQL_EXPORT
    sql: STRING_32
do
    create export.make (db)
    sql := export.to_sql ("users")
    -- Result:
    -- CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT);
    -- INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');
    -- INSERT INTO users VALUES (2, 'Bob', 'bob@example.com');
    -- ...
end
Large Exports

For very large tables, use file-based exports (to_csv_file, etc.) to avoid memory issues.