0

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.

0

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply! So I understand how to get the connection to the database, but I am now stuck on how to perform reads from it. I only want to perform queries on the database but I am really lost on what to do next.
FYI - you do not need to use a 3rd party library to use SQLIte.
-1

An example based on Xcode 15.0.x of how to bundle this.

I have file structure like this, Resources is under project directly

enter image description here

  1. Right click project name and choose "Add Files to xxx"
  2. Choose the db file you want to bundle, select below options: "Destination: Copy items if needed", "Added folders: Create folder references"

enter image description here

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".

3 Comments

An answer should stand on its own. If the other answer is ever deleted, your answer would be very incomplete.
@HangarRash, updated, can be a independent answer now
Your answer only shows how to add the db file to the project. Your answer in no way shows how to access its data. The question states that the OP already has the db file in the app bundle. The question is asking how to access its data. So far your answer is providing info that the question isn't asking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.