Android Question Use Variable in SQL Where Clause

Startup

Active Member
Licensed User
Longtime User
How can I use a variable in a SQL where Clause?

The following code works ok with the where clause containing a literal -

Cursor1=SQL1.ExecQuery("SELECT number from testtable where word = 'house'")

but the following code with the same literal in a variable (wordselectstring) produces an error -

Cursor1=SQL1.ExecQuery("SELECT number from testtable where word = wordselectstring")

Here is the code which I used to create the literal in the variable. The log shows the variable wordselectstring contains the literal 'house'

Dim wordselect As String
wordselect="house"
Dim wordselectstring As String
wordselectstring ="'" & wordselect & "'"
Log(wordselectstring)
 

DonManfred

Expert
Licensed User
Longtime User
Cursor1=SQL1.ExecQuery("SELECT number from testtable where word = wordselectstring")
try it with
B4X:
Cursor1=SQL1.ExecQuery("SELECT number from testtable where word = "&wordselectstring)

You cannot put variables in a string...
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
or use the .ExecQuery2 method ..
B4X:
Cursor1=SQL1.ExecQuery2("SELECT number from testtable where word = ?",Array As String(wordselect))
 
Upvote 0
Top