0

I have created SQLite DB file and filled it with data in an App in simulator and now I need the same database and filled data in a new project. I have copied the SQLite file to new project but every time I update a row (delete/edit) the data is updated until the next run and after rerunning the project my data and SQLite file is like it has been freshly copied to the project.

P.S.: The "clearData(_ SID: Int)" function deletes a row but after rerunning the project in xcode the row is there...

Here is my database structure:

import SQLite

var db: Connection!
let sherTable = Table("Sher");
let ID = Expression<Int>("SID");
let Fname = Expression<String?>("Fname");
let Ename = Expression<String?>("Ename");
let Flike = Expression<Bool?>("Flike");
let Elike = Expression<Bool?>("Elike");

let byteTable = Table("Byte");
let BID = Expression<Int>("BID");
let BSID = Expression<Int>("SID");
let Byte = Expression<String?>("Byte");
let searchText = Expression<String?>("searchText");
let BIsFarsi = Expression<Bool?>("IsFarsi");

let maniTable = Table("Mani");
let MID = Expression<Int>("MID");
let MSID = Expression<Int>("SID");
let Word = Expression<String?>("Word");
let Meaning = Expression<String?>("Meaning");
let MIsFarsi = Expression<Bool?>("IsFarsi");

func connectToDB() {
    do {
        let path = Bundle.main.path(forResource: "hafezDb", ofType: "sqlite3")!;

        let tmp = try Connection(path);
        db = tmp;
    } catch {
        print(error);
    }
}

func clearData(_ SID: Int) {
    let sher = sherTable.filter(ID == SID);
    let u = sher.update(Flike <- !Flike);
    do {
        try db.run(u);
    } catch {
        print(error)
    }
}
2
  • 1
    Are you testing the simulator or a real device? Commented Dec 11, 2018 at 16:54
  • @rmaddy in simulator... Commented Dec 11, 2018 at 17:03

1 Answer 1

2

You must be running in the simulator.

An app's bundle is read-only on a real device but it is writable in the simulator.

So your app appears to work in the simulator. But then you do another build and the prefilled database file in your project gets put back into the built app each time you run it and you start over with the original data again.

On a real device, your app would fail with errors about the database being read-only.

What you need to do on app startup is copy the read-only database file from the app bundle into a writable part of the app sandbox, such as the Documents folder. Of course you would first check if the file is there or not in the Documents folder and only copy it if it isn't there.

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

3 Comments

Is it possible to put the SQLite file in documents before release? I mean I don't want my app to take too much free space... I mean not copy it on first run from Bundle to Documents and put it there at the begining...
No. The Documents folder doesn't exist until the app is installed on the user's device.
I knew that I don't know why I asked such a question... But I wanted to just say you helped me twice and both were perfect... tnx

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.