Android Question IN OPERATORE LIKE SQL

Gianni Sassanelli

Active Member
Licensed User
Longtime User
Does b4a support in operator like SQL IN OPERATOR?

for Example:
B4X:
If RoleName in ( "Val1",  "Val2" ,"Val2" ) Then
       'Go
End If

Instead of:
B4X:
If RoleName = "Val1" Or RoleName = "Val2" Or RoleName = "Val2" Then
       'Go
End If
thanks
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
Dim lstValues As List
lstValues.Initialize2(Array As String("Val1", "Val2", "Val3"))
If lstValues.IndexOf("Val2") <> - 1 Then
' ...
End If
I do not think your solution is correct because he is looking for more than just Val2. He is looking for Val1 or Val2 or val3
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I do not think your solution is correct because he is looking for more than just Val2. He is looking for Val1 or Val2 or val3

??? :eek:

I wrote:
If lstValues.IndexOf("Val2") <> - 1 Then

but, of course, he can write:
If lstValues.IndexOf(RoleName) <> - 1 Then

getting the same of:
If RoleName in ( "Val1", "Val2" ,"Val2" ) Then
or:
If RoleName = "Val1" Or RoleName = "Val2" Or RoleName = "Val2" Then
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If IsIn("Val1", Array("Val1", "Val2", "Val3")) Then
Log("Yes")
End If

When you use the IN operator in SQLite, you use a code like this:
B4X:
curs=Starter.SQL1.ExecQuery2("SELECT  * FROM tblVehicles WHERE make =? OR make=? OR make=? ORDER BY Make", _
    Array As String("Val1","Val2","Val3") )
or this code:
B4X:
curs=Starter.SQL1.ExecQuery2("SELECT  * FROM tblVehicles WHERE make IN (?,?,?) ORDER BY Make", _
    Array As String("Val1","Val2","Val3") )
But @Erel and @LucaMs are presenting only one value: Val1. Do you have to run your function for each of Val1, Val2 and Val3. I still do not see the logic behind your code when the thread question is looking for any of the 3 values. Please explain.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The question was:

Does b4a support in operator like SQL IN OPERATOR?

"Does b4a support", which means: "is there a function similar to...?"

The "in" SQL (not SQLite operator) is:
upload_2018-7-29_17-52-37.png


So, the "b4a equivalent function" is exactly what Erel wrote (just after my great suggestion :p)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
But @Erel and @LucaMs are presenting only one value: Val1.
Maybe I can explain better what I mean so:

curs=Starter.SQL1.ExecQuery2("SELECT * FROM tblVehicles WHERE make IN (?,?,?) ORDER BY Make", _
Array As String("Val1","Val2","Val3") )
You also are comparing just one value, here: the current value of "make" (or, better, the value of "make" of the "current" (each) record.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You also are comparing just one value, here: the current value of "make"
Your statement is true, but the value can be any one of; "Val1", "Val2", or "Val3". Do you have to run your function for each of Val1, Val2 and Val3.
Does your function need to run this code 3 times:
B4X:
If IsIn("Val1", Array("Val1", "Val2", "Val3")) Then
       Log("Yes")
   End If
If IsIn("Val2", Array("Val1", "Val2", "Val3")) Then
       Log("Yes")
   End If
If IsIn("Val3", Array("Val1", "Val2", "Val3")) Then
       Log("Yes")
   End If
Perhaps it is best to wait and see what the creator of the thread intended to mean. If he is in tune with yours and Erel's solution, I will stand corrected.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Your statement is true, but the value can be any one of; "Val1", "Val2", or "Val3". Do you have to run your function for each of Val1, Val2 and Val3.
Does your function need to run this code 3 times

Why? I just need (he needs) to know if a value is IN a list of values or, saying it in a "reverse way", if a list of values contains a specific (specified) value.

The difference with SQL is that you perform this check on each record of a table.
 
Upvote 0
Top