Closed svenover closed 2 years ago
Hi,
I add the following line in the update and delete function - all is working, why?
self.users = Table("users")
When I use Table("users") instead of self.users all is working well.
// function to delete user
public func deleteUser(idValue: Int64) {
do {
// get user using ID
let user: Table = Table("users").filter(id == idValue)
// run the delete query
try db.run(user.delete())
} catch {
print(error.localizedDescription)
print(error)
}
}
Sorry, I don't understand it. The users object (table) is initiated at startup and when I check the count of it, it tells me the exact row count.
Strange.
Here both function.
// function to update user
public func updateUser(idValue: Int64, nameValue: String, emailValue: String, ageValue: Int64) {
do {
self.users = Table("users")
// get user using ID
let user: Table = self.users.filter(self.id == idValue)
// run the update query
try self.db.run(user.update(self.name <- nameValue, self.email <- emailValue, self.age <- ageValue))
} catch {
print(error.localizedDescription)
print(error)
}
}
// function to delete user
public func deleteUser(idValue: Int64) {
do {
self.users = Table("users")
// get user using ID
let user: Table = self.users.filter(id == idValue)
// run the delete query
try db.run(user.delete())
} catch {
print(error.localizedDescription)
print(error)
}
}
I found the issue. I have a function which gets all the users. Inside this function I do a sort / order of the users. This sort / order is the problem. Now, I do it in another way and now all is working.
Hi,
I use the link https://adnan-tech.com/create-read-update-and-delete-from-sqlite-swift-swift-ui/ to understand, how I have to use sqlite in my swiftui app.
All is fine with this example but with every call of the class, the init methode is called.
Therefore I switched to an observable class. So the init is called once at the app start.
With this change, the insert is working but the delete function failed with error in the title as well the update function too.
Maybe an expert, can tell me what I'm doing wrong. Many thanks in advance.
Here are my SQLite Class
Many thanks for the help. Sven