stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.64k stars 1.56k forks source link

CSV file support #984

Closed avnerbarr closed 3 years ago

avnerbarr commented 4 years ago

i'd like to load large CSV files into virtual sqlite tables. I'm able to do so using the sqlite3 mac application using the following commands and am curious if I can accomplish something similar without the need of iterating over the csv file + parsing etc.

sqlite> .mode csv
sqlite> .import /path/to/big-file.csv my_table
sqlite> select * from my_table;

was wondering if this is also possible doing something like this (which didn't work unfortunately)

let db = try! Connection()
let fname = "/path/to/big-file.csv"
try! db.execute(".mode csv")
try! db.execute(".headers on")
try! db.execute(".import \(fname) t1")

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: near ".": syntax error (code: 1)
2020-02-02 13:35:47.258603+0200 Datafeeds-that-works[21133:776001] [logging] near ".": syntax error in ".mode csv"

also tried according to this:

https://www.sqlite.org/csv.html

try! db.execute("CREATE VIRTUAL TABLE t1 USING csv(filename='\(fname)')")
zachwick commented 4 years ago

@avnerbarr I don't believe that you can do this as you're attempting using the sqlite application commands. Instead, I might suggest using a CSV parsing library to load your data and interact with it.