0

I'm new to all this, but I've managed to finish coding my first simple Swift/SwiftUI App. It includes a SQlite DB that I've pre-populated with my data, and I manage it using SQLite.swift. The app is working fine using the iPad simulator in XCode. I tried to test the App on a physical device (iPad 7th generation), but the DB doesn't transfer (or if it is transferring, it isn't recognized). Instead, the App on the iPad creates a new DB with no records. I confirmed that the DB is in the Documents folder when run in the simulator, but the same check on the iPad confirms that there's only a new empty DB.

Here are the first few relevant lines of the function I'm using to find the DB:

func conceptListForAuthor(chosenAuthorIndex: Int, chosenConceptIndex: Int) -> Array<String>{
       
    do{
        let path = NSSearchPathForDirectoriesInDomains(
         .documentDirectory, .userDomainMask, true
        ).first!
        let quoteDB = try Connection("\(path)/quoteDB.sqlite3")

I've searched extensively for an answer, and even submitted a tech support request on Apple Developer, but with no response. I found one question on stackoverflow that seems to address this question, but for an app that doesn't use SQLite.swift, so I'm not sure how to use the recommended solution.

Is there a way to code my app so the DB will be recognized when tested on the iPad and prevent the app from creating a new empty DB?

randombits
  • 21
  • 3

2 Answers2

0

Why would the database be transferred? The simulator and your iPad are two different devices with two different environments.

If you use a pre-populated database, can't you save the database creation script, and run it when you start your app to create your data on any new device you will test.

I think that's how I would do.

Moose
  • 2,607
  • 24
  • 23
0

Drag Sqlite database to your project and make sure it is included in the target. then:

func openDatabase() -> OpaquePointer? {
    if sqlite3_open(Bundle.main.path(forResource: "quoteDB", ofType: "sqlite3"), &db) == SQLITE_OK {
        //            print("Successfully opened connection to database")
    } else {
        print("Unable to open database. Verify that you created the directory described " +
            "in the Getting Started section.")
    }
    return db
}
Objective C
  • 21
  • 1
  • 5