1

I use this SQLite library and want to delete every row in the table that contains the same email address. So let's say that 100 users is registred with the email: test@test.com, and I want to delete all of them. According to the documentation we can do this:

let alice = users.filter(id == 1)
try db.run(alice.delete())
// DELETE FROM "users" WHERE ("id" = 1)

But the ID's is different for each user. So is it possible to do something like: // DELETE FROM "users" WHERE ("email" = test@test.com).

The email column in the table is a string. Like this: let email = Expression<String>("email")

EDIT:

I tried this, but somehow it deleted all of my rows, not only those who contains the email address:

let alice = users.select(self.email == emails[indexPath.row])
                let deleteUser = user.delete()
                do {
                    try self.database.run(deleteUser)
                } catch {
                    print(error)
                }
  • 2
    `delete from users where email = 'test@test.com'` – MadProgrammer Oct 07 '18 at 20:45
  • @MadProgrammer How do I do this in my code? – Laura Lexis Oct 07 '18 at 20:58
  • 1
    `if sqlite3_prepare_v2(db, "delete from users where email = ?", -1, &statement, nil) == SQLITE_OK { if sqlite3_bind_text(statement, 1, "test@test.com", -1, SQLITE_TRANSIENT) != SQLITE_OK {...}` – MadProgrammer Oct 07 '18 at 21:01
  • SO is not a replacement for good tutorials - maybe start with [SQLite With Swift Tutorial: Getting Started](https://www.raywenderlich.com/385-sqlite-with-swift-tutorial-getting-started); [Swift SQLite Tutorial for Beginners – Using SQLite in iOS Application](https://www.simplifiedios.net/swift-sqlite-tutorial/); [Accessing an SQLite Database in Swift](https://stackoverflow.com/questions/24102775/accessing-an-sqlite-database-in-swift) – MadProgrammer Oct 07 '18 at 21:02
  • But personally, I'd look at any of the available wrapper libraries, like [stephencelis/SQLite.swift](https://github.com/stephencelis/SQLite.swift) - but that's because I'm lazy – MadProgrammer Oct 07 '18 at 21:03
  • @MadProgrammer Updated code – Laura Lexis Oct 07 '18 at 21:39

1 Answers1

0

Try this:

let alice = users.filter([emails[indexPath.row]].contains(self.email))
                    let deleteUser = user.delete()
                    do {
                        try self.database.run(deleteUser)
                    } catch {
                        print(error)
                    }
Erik Auranaune
  • 1,224
  • 1
  • 8
  • 17