Reputation: 1289
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)
}
}
Upvotes: 0
Views: 61
Reputation: 318854
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.
Upvotes: 2