0

I am trying to delete multiple entries from an SQLite database.

This is the code I am using

Public Function RemoveUsers(_users As List(Of User)) As Integer
            Using conn As New SQLiteConnection(connectionString)
                Dim removeUserQuery As String = "DELETE FROM Users WHERE userId IN (@userIds)"
                Dim cmd As New SQLiteCommand(removeUserQuery, conn)
                Dim listOfIds As New List(Of Integer)

                For Each user As User In _users
                    listOfIds.Add(user.UserID)
                Next

                cmd.Parameters.AddWithValue("@userIds", String.Join(",", listOfIds))
                Console.WriteLine(cmd.CommandText)
                conn.Open()
                Dim i As Integer = cmd.ExecuteNonQuery()
                conn.Close()
                Return i
            End Using
        End Function

The query will end up something like

DELETE FROM Users WHERE userId IN (2, 4, 5, 7, 9)

But I am always getting 0 rows affected. I tried it in DB Browser for SQLite and it worked. I assume its something to do with cmd.Parameters.AddWithValue() but I am not sure. Thanks in advance.

  • 1
    You cannot use a single parameter to represent the multiple values required by an IN clause. You need a parameter for each value. The duplicate explains it better and gives you some solutions – Steve Feb 09 '20 at 15:53
  • Aight will check out the post. Thanks! – YeetlordTheFirst Feb 09 '20 at 15:54

0 Answers0