Reputation: 11
I am a Swift newbie currently working on an IOS application, which needs to access an existing static database (size 400kb) of 1 table with about 3300 rows, and 26 columns. This database was initially a CSV file, converted into an SQLite file.
From forums I read online, it is to my understanding that SQLite queries are incredibly fast, and for that reason should be the preferred choice. I will only need to be reading the database and will not perform any writes.
I have found a number of old tutorials online which have utilized Objective-C or tutorials from several years ago which utilize swift 2 or 3. A newer solution I found was to use this SQLite wrapper: https://github.com/stephencelis/SQLite.swift. However, I am not entirely sure how to implement it. I was wondering if it was possible to read directly from my bundled database, or if I had to create a new table and copy from the bundled database.
Upvotes: 0
Views: 1419
Reputation: 214
An example based on Xcode 15.0.x of how to bundle this.
I have file structure like this, Resources is under project directly
If you select "Copy items if needed", Xcode will check whether the resource file already exists in the Bundle when building the project. If it does not exist, it will copy the resource file to the Bundle.
If you select "Added folders: Create folder references",Xcode creates a folder reference in the project instead of copying the folder's contents directly into the project. Folder references maintain the folder hierarchy and structure of the folder when adding a set of files.
These options can be set according to the needs of the project. Normally, if you want resource files to be copied into the Bundle when building the project and do not need to maintain the folder hierarchy, you can select "Copy items if needed" and uncheck "Create folder references". If you wish to maintain the folder hierarchy and do not need to copy files into the Bundle, you can select "Create folder references".
Upvotes: -1
Reputation: 2762
It looks like SQLite.swift
will get the job done for you. From the documentation on the page you linked it looks like you use Connection(pathToDB)
to start using the library.
Since you're bundling the database and don't plan on modifying it you should be able to use Bundle.main.path(forResource:, ofType:)
to get the path to your bundled database. If you want to modify it, you'll probably want to copy it to the documents directory and then reference that copy.
Another good answer on getting paths to various kinds of bundle resources if you need more help: How to get path of image form Resource of main bundle
Upvotes: 2