Pass array to IN clause as parameter NQL1 on CBLite

I am trying to do a n1ql query in couchbaselite, where I am passing a parameter which is an Array, this to use it in the “IN” clause. But I am getting an error, what is the correct way to do it?

This is my parameter that I wanna use types: Array<Int?>

                val query = db.createQuery(
                        """
                            SELECT * FROM _
                            WHERE type IN \$types
                        """.trimIndent()
                )

                val typesFiltered = MutableArray().apply {
                    types.forEach { type ->
                        type?.let { addInt(it) }
                    }
                }

                query.parameters = Parameters().setArray("types", typesFiltered)

What error are you getting?

Failed creating query N1QL syntax error near character 31
(CouchbaseLite Android v3.1.6-2@34 (CE/release, Commit/unofficial@06831584eaca Core/3.1.6 (6) at 2024-02-28T22:56:07.957862808Z) on Java; Android 13; SM-A225M)

This happens when I pass the parameters to “query.parameters”

I wondef if it needs the square brackets around the arg …

IN [ $types ] …

A little bit of Googling turned up this - which seems to indicate that the square brackets are required

Actually, It was my bad I made a mistake by using “”" $types “”" In this way on Kotlin was actually concatenating my variable and not as an argument… So, I changed it to a normal string “ [$types]". Thus it is interpreted as the use of an argument to the IN clause in this case

1 Like