Android Question select id list form tabel in sql

SMOOTSARA

Active Member
Licensed User
Longtime User
Hello friends🌹

I use the following command to get a specific sequence in the database
For example, where the ID is 1

cur=sql.ExecQuery("SELECT * FROM tabel1 WHERE di = 1 ")

My question is here

I get a list of different IDs as follows
List_id =>
[1,23,467,346,83685,2568,2]

What syntax should I use to get these lines in the database?


Thanks 🙏
 
D

Deleted member 103

Guest
Hello friends🌹

I use the following command to get a specific sequence in the database
For example, where the ID is 1



My question is here

I get a list of different IDs as follows
List_id =>


What syntax should I use to get these lines in the database?


Thanks 🙏
Maybe like this?

cur=sql.ExecQuery("SELECT * FROM tabel1 WHERE di = 1 ")
B4X:
cur=sql.ExecQuery("SELECT * FROM tabel1 WHERE id ='1'")

or this ?
dim id as int = 1
cur=sql.ExecQuery("SELECT * FROM tabel1 WHERE id =' & id & "'")
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It is a mistake to build a query like this.

Better is to use parametrized Queries.
B4X:
cur=sql.ExecQuery2("SELECT * FROM tabel1 WHERE id =?;",Array(1))
 
Upvote 0
D

Deleted member 103

Guest
It is a mistake to build a query like this.

Better is to use parametrized Queries.
such nonsense!
If only the "id" is required, then you don't have to use parametrized queries.
 
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
Hello friends🌹

I use the following command to get a specific sequence in the database
For example, where the ID is 1



My question is here

I get a list of different IDs as follows
List_id =>


What syntax should I use to get these lines in the database?


Thanks 🙏
I don't actually understand your question but if you need only a certain number of lines you can use something like

B4X:
cur=sql.ExecQuery("SELECT * FROM tabel1 WHERE di in (1,23,467,346,83685,2568,2) ")
OR
cur=sql.ExecQuery2("SELECT * FROM tabel1 WHERE id in (?,?,?,?,?,?,?);",Array(1,23,467,346,83685,2568,2))

if not... and you just need one line then post #2 is your answer.
 
Upvote 0

SMOOTSARA

Active Member
Licensed User
Longtime User
Hello friends
I still haven't been able to solve this problem:(

I get a different list of IDs each time and I have to select them in the database

This list may have 3 members exp=>list1=>[34,674,3]
Or it may have 20 members exp=>list1=>[34,4,6,7,45,578,123,34,47,547,1,6,2,8,4,2,5,7,4,3,2]

How can I introduce the list to Syntax Database?

B4X:
cur=sql.??????? ("SELECT * FROM tabel1 WHERE id in ???????????????????????
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
How can I introduce the list to Syntax Database?
Use the stringBuilder (internal library part of B4A) to build the IN operator clause for any list of number of items and then build your query. Here is an example on how to do it:
B4X:
Dim MyList As List
    MyList.Initialize
    MyList.AddAll(Array As Int(34,674,3, 76,33,98,4))
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append( " IN(")
    For i= 0 To MyList.size -1
        Dim f As Int =MyList.Get(i)
        If i = MyList.Size-1 Then
            sb.Append($"${f})"$)
        Else
            sb.Append($"${f},"$)
        End If
    Next
    Log(sb.ToString)  'displays as example: IN(34,674,3,76,33,98,4)
    Dim MyInClause As String = sb.ToString
    cur=sql.ExecQuery("SELECT * FROM tabel1 WHERE id " & MyInClause )
 
Upvote 0
Top