Questions about ExecQuery2

parijs

Active Member
Licensed User
Longtime User
I have:
B4X:
Dim jop As String
Dim jop1 As String
.
.
jop= "1"
jop1="2"

oRst = SQL.ExecQuery2("SELECT MapNaam, arronnr, wijk, keuze FROM visit WHERE arronnr = ?", Array As String (jop OR jop1))

And the error is Cannot parse: 1 as boolean
The type of arronnr = TEXT in the db

And how many OR can I use

Last question
Is it be possible to use for ("SELECT MapNaam Mapnaam as a Variable
and
Is it be possible to use for WHERE arronnr = arronnr as a Variable

:sign0013:
 
Last edited:

parijs

Active Member
Licensed User
Longtime User
Hi Erel,

I use your code and there is a view but everything is in it not only the 1 and 2 but also the 5, 6, 7, 8 .....

The other questions:

Is this possible?

B4X:
Dim B As String
B = "arronnr"

oRst = SQL.ExecQuery2("SELECT MapNaam, arronnr, wijk, keuze FROM visit WHERE B = ? OR ?", Array As String (jop, jop1))


or this

B4X:
Dim B As String
B = "MapNaam"

oRst = SQL.ExecQuery2("SELECT B, arronnr, wijk, keuze FROM visit WHERE arronnr = ? OR ?", Array As String (jop, jop1))

Thanks for your reply
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
SQL.ExecQuery2("SELECT MapNaam, arronnr, wijk, keuze FROM visit WHERE B = ? OR ?", Array As String (jop, jop1))
This will not work as the B inside the string is not related in any way to the variable named B.

This code will work:
B4X:
oRst = SQL.ExecQuery2("SELECT MapNaam, arronnr, wijk, keuze FROM visit WHERE " & B &"  = ? OR ?", Array As String (jop, jop1))

oRst = SQL.ExecQuery2("SELECT B, arronnr, wijk, keuze FROM visit WHERE arronnr = ? OR ?", Array As String (jop, jop1))
This code should work.
 
Upvote 0

parijs

Active Member
Licensed User
Longtime User
Thanks Erel,

Have you any idea why your code

B4X:
oRst = SQL.ExecQuery2("SELECT MapNaam, arronnr, wijk, keuze FROM visit WHERE arronnr = ? OR ?", Array As String (jop, jop1))

Gif me all of the db
 
Last edited:
Upvote 0

parijs

Active Member
Licensed User
Longtime User
My excuse I did not know where I could find that.
But now I found the [ code ]

Tanks for all
 
Upvote 0

parijs

Active Member
Licensed User
Longtime User
Back again :sign0013:

I have tried your code

B4X:
myvar="keuze"
cur = SQL.ExecQuery("SELECT myvar FROM maandag")
But it give me "no such column"

Also I have a problem with

B4X:
Main.dagkeuze ="maandag"

cur = SQL.ExecQuery("SELECT keuze FROM Main.dagkeuze")      
intCount = SQL.ExecQuerySingleResult("SELECT count(*) FROM  Main.dagkeuze")

looked everywhere but can not find meaningful solutions.
So my hope lies with the Master.
 
Upvote 0

jcmartini

Member
Licensed User
Longtime User
ExecQuery2 escaping characters

I tried to use ExecQuery2 with a query using a virtual SQLite table. It seems to me that asterisk raises an error:

SQL1.ExecQuery2("SELECT fielda, fieldb FROM table WHERE fieldb MATCH '?'", Array As String(string & "*")

I didn't find any definitions for character escaping, how it works and for which character.
 
Upvote 0

jcmartini

Member
Licensed User
Longtime User
I got:

Parsing code. Error
Error parsing program.
Error description: Syntax error.

NB: it's not the same SQL you show.
 
Upvote 0

arjian

Member
Licensed User
Longtime User
HI
i got error in code bellow,help me to know why
B4X:
QueryNearPost="SELECT PostID,LocalName,LocalAddress,OwnerType,Capacity,Latitude,Longitude FROM " & PostTableName & " WHERE PostID = ?"
 
    Dim PostIDArray(NearPostsIDList.Size) As String
    For i=0 To NearPostsIDList.Size-1
        PostIDArray(i)=    NearPostsIDList.Get(i)
    Next
    CurNearPost=Main.SQL1.ExecQuery2(QueryNearPost,PostIDArray)
 

Attachments

  • ExecQuery2.png
    ExecQuery2.png
    22.1 KB · Views: 180
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Your array has 3 elements. You need 3 arguments. See code below:
B4X:
QueryNearPost="SELECT PostID,LocalName,LocalAddress,OwnerType,Capacity,Latitude,Longitude FROM " & PostTableName & " WHERE PostID = ? OR ? OR ?"
 
Last edited:
Upvote 0

jsanchezc

Member
Licensed User
Longtime User
Your array has 3 elements. You need 3 arguments. See code below:
B4X:
QueryNearPost="SELECT PostID,LocalName,LocalAddress,OwnerType,Capacity,Latitude,Longitude FROM " & PostTableName & " WHERE PostID = ? OR ? OR ?"
You can also use IN instear contatenate several OR:
QueryNearPost="SELECT PostID,LocalName,LocalAddress,OwnerType,Capacity,Latitude,Longitude FROM " & PostTableName & " WHERE PostID IN (?,?,?)"
IN allow you to compare field value with a list of values (one or more values).

SELECT * FROM COUNTRIES WHERE NAME IN('FRANCE','BELGIUM','ITALY')
SELECT * FROM COUNTRIES WHERE ID IN (1,4,5,8,9,10)

If you have a variable number of arguments you can use ExecQuery and
create the sql string dinamicaly:

SQLSTR="SELECT * FROM COUNTRIES WHERE ID IN("
FOR I=1 TO 10
SQLSTR=SQLSTR & I
IF I < 10 THEN
SQLSTR=SQLSTR & ","
END IF
NEXT I
SQLSTR=SQLSTR & ") "
then call QueryExec
 
Upvote 0

arjian

Member
Licensed User
Longtime User
You can also use IN instear contatenate several OR:
QueryNearPost="SELECT PostID,LocalName,LocalAddress,OwnerType,Capacity,Latitude,Longitude FROM " & PostTableName & " WHERE PostID IN (?,?,?)"
IN allow you to compare field value with a list of values (one or more values).

SELECT * FROM COUNTRIES WHERE NAME IN('FRANCE','BELGIUM','ITALY')
SELECT * FROM COUNTRIES WHERE ID IN (1,4,5,8,9,10)

If you have a variable number of arguments you can use ExecQuery and
create the sql string dinamicaly:

SQLSTR="SELECT * FROM COUNTRIES WHERE ID IN("
FOR I=1 TO 10
SQLSTR=SQLSTR & I
IF I < 10 THEN
SQLSTR=SQLSTR & ","
END IF
NEXT I
SQLSTR=SQLSTR & ") "
then call QueryExec


in fact number of arguments is variable so i used this code
B4X:
For i=0 To NearPostsIDList.Size-1
        Dim    CurNearPost As Cursor
        QueryNearPost="SELECT LocalName,LocalAddress,OwnerType,Capacity,Latitude,Longitude FROM " & PostTableName & " WHERE PostID = '" & NearPostsIDList.Get(i) & "'"
        CurNearPost=Main.SQL1.ExecQuery(QueryNearPost)
.
.
now the question is which code is better?,i think your code will be executed in shorter time (PostID has variable arguments between 0 to 4000)
 
Last edited:
Upvote 0

jsanchezc

Member
Licensed User
Longtime User
in fact number of arguments is variable so i used this code
B4X:
For i=0 To NearPostsIDList.Size-1
        Dim    CurNearPost As Cursor
        QueryNearPost="SELECT LocalName,LocalAddress,OwnerType,Capacity,Latitude,Longitude FROM " & PostTableName & " WHERE PostID = '" & NearPostsIDList.Get(i) & "'"
        CurNearPost=Main.SQL1.ExecQuery(QueryNearPost)
.
.
now the question is which code is better?,i think your code will be executed in shorter time (PostID has variable arguments between 0 to 4000)


B4X:
Dim QueryNearPost as String=""
Dim    CurNearPost As Cursor
Dim NumberRows as int=0
'---------------------------------------------------------------------------
'If you avoid long code lines you'll see all code, all fields.
'If put comma field separator before each field (instad at the end of each field)
' you can add fields or rem a line without worry about comma
'----------------------------------------------------------------------------
QueryNearPost="SELECT            LocalName"
QueryNearPost= QueryNearPost & " ,LocalAddress"
QueryNearPost= QueryNearPost & " ,OwnerType "
QueryNearPost= QueryNearPost & " ,Capacity "
QueryNearPost= QueryNearPost & " ,Latitude "
QueryNearPost= QueryNearPost & " ,Longitude "
QueryNearPost= QueryNearPost & "  FROM " & PostTableName
QueryNearPost= QueryNearPost & " WHERE PostID IN ("
'--------------------------------------------------------------
NumberRows=NearPostsIDList.Size-1
For i=0 To NumerRows
       QueryNearPost= QueryNearPost & "''" & NearPostsIDList.Get(i) & "'"
       if i < NumberRows then
         'add , separator between IN list, except last item
         QueryNearPost= QueryNearPost & ","
      end if
Next
'Add close )
QueryNearPost= QueryNearPost & ")"
'If needed add order by and so on...
'QueryNearPost= QueryNearPost & " Order by LocalName"
Log(QueryNearPost)  'You can copy/paste from log list to external ExecQuery Tool to test
'Execute Query
CurNearPost=Main.SQL1.ExecQuery(QueryNearPost)

To improve speed, PostTableName should have index: PostId
 
Last edited:
Upvote 0
Top