Read large file into sqlite table in objective-C on iPhone

久未见 提交于 2019-12-03 21:32:19

Ugh... don't deal with the C API yourself. Especially not when there are nicer wrappers available: http://cocoaheads.byu.edu/resources/sqlite

I think you are confused about waht sqlite3 is. Sqlite3 is not an in memory database, it is a persistent disk based database, if you are loading all your data into every time you are doing something wrong. SQLite is designed for persistence, after you enter all the data into it you can just close the db, and the next time you open it using sqlite3_open_v2(), no need to reload all the data. By default sqlite3 does all its commits atomically (which is part of why the inserts are slow), so you don't even technically need to close it (though you still should, just to be safe, or in case you want to try to improve performance by using transactions).

In fact, rather than including a the CSV file with the app you could generate the sqlite3 database from the CSV file as part of your build process and include that with the app.

OK, I think I've got it. Starting from the Unix command prompt, the following reads in the CSV file and writes out a SQL file:

 % sqlite3
 sqlite3> .mode csv
 sqlite3>  create table NEWTABLE (ITEM_ID INTEGER PRIMARY KEY, FIELD0 TEXT, FIELD1 TEXT, FIELD2 TEXT, FIELD3 TEXT, FIELD4 TEXT, FIELD5 TEXT);
 sqlite3> .import csvfile.csv NEWTABLE
 sqlite3> .output csvfile.sql
 sqlite3> .dump NEWTABLE

The part I still haven't figured out is if there is a quick way to read the sql file into sqlite3. The approach I currently have is to read each line of the sql file and execute on the iPhone. Is there a one line approach to reading the sql file into sqlite3?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!