B4J Question How Convert query string to smart string

behnam_tr

Active Member
Licensed User
Longtime User
hi

how can i convert this string to smart string
i have a problem With ' character

B4X:
 Dim RS2 As ResultSet=Main.Sql1.ExecQuery(" Select kala.id,kala.catid,kala.name  WHERE kala.name LIKE '%"&stext&"%'  Or kala.id='"&stext&"' " )

B4X:
Dim RS2 As ResultSet=Main.Sql1.ExecQuery($"  Select kala.id,kala.catid,kala.name  WHERE kala.name LIKE ${'%stext%'}   Or kala.id=${text} "$)
 
Solution
Using parameterized query and string literals and also an alias for the table name:
B4X:
Dim RS2 As ResultSet=SQL.ExecQuery2($"SELECT K.id, K.catid, K.name FROM kala K WHERE K.name LIKE ? OR K.id =?"$ , Array As Object($"%${stext}%"$ , text))

emexes

Expert
Licensed User
The single quotes and % wildcards should be outside the the substitution delimiters eg:

B4X:
Dim RS2 As ResultSet=Main.Sql1.ExecQuery($"  SELECT  kala.id,kala.catid,kala.name  WHERE  kala.name LIKE '%${stext}%'  OR  kala.id=${text} "$)

ps is the second search term ${text} supposed to be enclosed in single quotes too? Or are kala.id and text numeric?
 
Last edited:
Upvote 1

emexes

Expert
Licensed User
I know it's an extra line, but it sure makes debugging easier if you gather the SQL query into a string first so that you can see what SQLite is seeing eg:

B4X:
Dim Query As String = $"SELECT kala.id, kala.catid, kala.name WHERE kala.name LIKE '%${stext}%' OR kala.id = ${text}"$
Log(Query)
Dim RS2 As ResultSet = Main.Sql1.ExecQuery(Query)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Using parameterized query and string literals and also an alias for the table name:
B4X:
Dim RS2 As ResultSet=SQL.ExecQuery2($"SELECT K.id, K.catid, K.name FROM kala K WHERE K.name LIKE ? OR K.id =?"$ , Array As Object($"%${stext}%"$ , text))
 
Upvote 1
Solution
Top