SIMPLE_SQL_IMPORT
Import data from CSV, JSON, or SQL files into database tables.
Creation
make (a_db: SIMPLE_SQL_DATABASE)
Create importer attached to database.
CSV Import
from_csv (a_table: READABLE_STRING_8; a_csv: READABLE_STRING_GENERAL)
Import CSV string into table. First row must be headers.
from_csv_file (a_table: READABLE_STRING_8; a_path: READABLE_STRING_GENERAL)
Import CSV file into table.
from_csv_with_options (a_table: READABLE_STRING_8; a_csv: READABLE_STRING_GENERAL; a_delimiter: CHARACTER; a_has_headers: BOOLEAN)
Import with custom delimiter and header option.
JSON Import
from_json (a_table: READABLE_STRING_8; a_json: READABLE_STRING_GENERAL)
Import JSON array of objects into table.
from_json_file (a_table: READABLE_STRING_8; a_path: READABLE_STRING_GENERAL)
Import JSON file into table.
SQL Import
from_sql (a_sql: READABLE_STRING_GENERAL)
Execute SQL statements (CREATE, INSERT, etc.).
from_sql_file (a_path: READABLE_STRING_GENERAL)
Execute SQL file.
Import Status
rows_imported: INTEGER
Number of rows imported in last operation.
last_error: detachable STRING_8
Error message if import failed.
Examples
Import CSV
local
import: SIMPLE_SQL_IMPORT
do
create import.make (db)
-- Create target table first
db.execute ("CREATE TABLE IF NOT EXISTS users (name TEXT, email TEXT)")
-- Import from CSV string
import.from_csv ("users", "name,email%NAlice,alice@example.com%NBob,bob@example.com")
print ("Imported " + import.rows_imported.out + " rows%N")
-- Or from file
import.from_csv_file ("users", "users_data.csv")
end
Import JSON
local
import: SIMPLE_SQL_IMPORT
json: STRING_8
do
create import.make (db)
json := "[{%"name%":%"Alice%",%"email%":%"alice@example.com%"},{%"name%":%"Bob%",%"email%":%"bob@example.com%"}]"
import.from_json ("users", json)
print ("Imported " + import.rows_imported.out + " rows%N")
end
Restore from SQL Dump
local
import: SIMPLE_SQL_IMPORT
do
create import.make (db)
-- Restore from previously exported dump
import.from_sql_file ("full_backup.sql")
if attached import.last_error as err then
print ("Import error: " + err + "%N")
else
print ("Import successful%N")
end
end
Import with Transaction
local
import: SIMPLE_SQL_IMPORT
do
create import.make (db)
-- Wrap large import in transaction for better performance
db.begin_transaction
import.from_csv_file ("logs", "large_log_file.csv")
if attached import.last_error then
db.rollback
print ("Import failed, rolled back%N")
else
db.commit
print ("Imported " + import.rows_imported.out + " rows%N")
end
end
Table Must Exist
CSV and JSON imports require the target table to exist with matching column names. SQL imports can create tables via CREATE statements.
Performance Tip
Wrap large imports in a transaction (db.begin_transaction / db.commit) for significantly better performance.