S Startup Active Member Licensed User Longtime User Dec 29, 2015 #1 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)
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 Dec 29, 2015 #2 Startup said: Cursor1=SQL1.ExecQuery("SELECT number from testtable where word = wordselectstring") Click to expand... try it with Code: Copy Cursor1=SQL1.ExecQuery("SELECT number from testtable where word = "&wordselectstring) You cannot put variables in a string... Upvote 0
Startup said: Cursor1=SQL1.ExecQuery("SELECT number from testtable where word = wordselectstring") Click to expand... try it with Code: Copy Cursor1=SQL1.ExecQuery("SELECT number from testtable where word = "&wordselectstring) You cannot put variables in a string...
mangojack Expert Licensed User Longtime User Dec 29, 2015 #3 or use the .ExecQuery2 method .. Code: Copy Cursor1=SQL1.ExecQuery2("SELECT number from testtable where word = ?",Array As String(wordselect)) Upvote 0
or use the .ExecQuery2 method .. Code: Copy Cursor1=SQL1.ExecQuery2("SELECT number from testtable where word = ?",Array As String(wordselect))
Erel B4X founder Staff member Licensed User Longtime User Dec 29, 2015 #4 DonManfred said: You cannot put variables in a string... Click to expand... Not 100% correct: https://www.b4x.com/android/forum/threads/50135/#content However you should use ExecQuery2 as @mangojack wrote. Upvote 0
DonManfred said: You cannot put variables in a string... Click to expand... Not 100% correct: https://www.b4x.com/android/forum/threads/50135/#content However you should use ExecQuery2 as @mangojack wrote.
DonManfred Expert Licensed User Longtime User Dec 29, 2015 #5 Erel said: Not 100% correct Click to expand... yes, with the string literal it works... But you you need to use a ${}-Command to include a variable into the literal Upvote 0
Erel said: Not 100% correct Click to expand... yes, with the string literal it works... But you you need to use a ${}-Command to include a variable into the literal
S Startup Active Member Licensed User Longtime User Dec 29, 2015 #6 mangojack said: or use the .ExecQuery2 method .. Code: Copy Cursor1=SQL1.ExecQuery2("SELECT number from testtable where word = ?",Array As String(wordselect)) Click to expand... That does it. Thanks! Upvote 0
mangojack said: or use the .ExecQuery2 method .. Code: Copy Cursor1=SQL1.ExecQuery2("SELECT number from testtable where word = ?",Array As String(wordselect)) Click to expand... That does it. Thanks!