sqlite_orm icon indicating copy to clipboard operation
sqlite_orm copied to clipboard

CSV import to populate database

Open ElephantInTheRoomFirst opened this issue 9 years ago • 4 comments

I am considering to use CSV file to populate database.

There are two choices to do that

  1. Use build-in facility of sqlite to import CSV format. I tried to find some examples to deal with it, but could not find any useful.
  2. Parsing and doing insert/replace on database: This option will have to read and parse CSV. It is kind of error-prone.

What do you think about option 1? Do you know how to implement it?

ElephantInTheRoomFirst avatar Jul 17 '17 18:07 ElephantInTheRoomFirst

sqlite_orm is an sqlite frontend not CSV parsing lib. To solve your issue you need to get CSV parser, next parse it line by line in a loop. In every iteration create a data model object and insert it. To improve it insert values within a transaction. If you doubt about this method assume you don't have sqlite_orm but have only libsqlite3. You'd parse CSV with CSV parser and next created queries with SQLite C interface and run it. So I repeat sqlite_orm isn't a CSV parser. But you can stick together CSV parser with sqlite_orm to solve your issue.

fnc12 avatar Jul 18 '17 05:07 fnc12

Thanks. I was asking you about 8. CSV Import from https://sqlite.org/cli.html If you know any easier way to do it.

ElephantInTheRoomFirst avatar Jul 18 '17 16:07 ElephantInTheRoomFirst

Ok I see. The point is that .import and .mode csv are implemented only in sqlite3 command shell not in C API. More info here

fnc12 avatar Jul 18 '17 17:07 fnc12

Thanks. Then I close this topic as out of scope.

ElephantInTheRoomFirst avatar Jul 20 '17 18:07 ElephantInTheRoomFirst