I know that there is a command line ".import" which can do this. But if I want to do the same thing in C programs, what should I do? And we are using the SQLite main memory database. It means we have to do all things in one program. Otherwise, the database is cancelled after the connection is closed. Thank you.
1 Answer
The SQLite 3 C - API can be used to import csv into an Sqlite3 database.
- Your C code needs to open the CSV file, read in each line of the CSV file, making sure to store the column values (in the current row) in an appropriate container (i.e. column boundaries determined by comma separator).
- These column values can be bound to parameters in a prepared statement and then committed to the database. Here is a useful link: http://blog.quibb.org/2010/08/fast-bulk-inserts-into-sqlite/
2 Comments
Keith Thompson
Tokenizing CVS input is non-trivial. Simple cases are simple, but I don't think there's even a universally recognized standard for, for example, how to represent fields that contain commas. See en.wikipedia.org/wiki/Comma-separated_values
Jens A. Koch
There is a standard: datatracker.ietf.org/doc/html/rfc4180