Sergey_New Well-Known Member Licensed User Longtime User Feb 15, 2023 #1 Returning some records from a table B4X: Copy sql1.Initialize(my_path, "my_tags.db", False) Dim rs As ResultSet Dim str As String="xxx" rs = sql1.ExecQuery("SELECT key FROM tags WHERE person=1") Please help me change rs to detect the absence of str in the key field.
Returning some records from a table B4X: Copy sql1.Initialize(my_path, "my_tags.db", False) Dim rs As ResultSet Dim str As String="xxx" rs = sql1.ExecQuery("SELECT key FROM tags WHERE person=1") Please help me change rs to detect the absence of str in the key field.
Solution JCO Feb 15, 2023 If I understand your request correctly, it would be something like this: B4X: Copy sql1.Initialize(my_path, "my_tags.db", False) Dim rs As ResultSet Dim str As String="xxx" rs = sql1.ExecQuery2("SELECT key FROM tags WHERE person=1 AND key<>?", Array(str)) Or better: B4X: Copy Dim str As String="xxx" Dim person as int = 1 rs = sql1.ExecQuery2("SELECT key FROM tags WHERE person=? AND key<>?", Array(person, str))
If I understand your request correctly, it would be something like this: B4X: Copy sql1.Initialize(my_path, "my_tags.db", False) Dim rs As ResultSet Dim str As String="xxx" rs = sql1.ExecQuery2("SELECT key FROM tags WHERE person=1 AND key<>?", Array(str)) Or better: B4X: Copy Dim str As String="xxx" Dim person as int = 1 rs = sql1.ExecQuery2("SELECT key FROM tags WHERE person=? AND key<>?", Array(person, str))
JCO Active Member Licensed User Longtime User Feb 15, 2023 #2 If I understand your request correctly, it would be something like this: B4X: Copy sql1.Initialize(my_path, "my_tags.db", False) Dim rs As ResultSet Dim str As String="xxx" rs = sql1.ExecQuery2("SELECT key FROM tags WHERE person=1 AND key<>?", Array(str)) Or better: B4X: Copy Dim str As String="xxx" Dim person as int = 1 rs = sql1.ExecQuery2("SELECT key FROM tags WHERE person=? AND key<>?", Array(person, str)) Upvote 0 Solution
If I understand your request correctly, it would be something like this: B4X: Copy sql1.Initialize(my_path, "my_tags.db", False) Dim rs As ResultSet Dim str As String="xxx" rs = sql1.ExecQuery2("SELECT key FROM tags WHERE person=1 AND key<>?", Array(str)) Or better: B4X: Copy Dim str As String="xxx" Dim person as int = 1 rs = sql1.ExecQuery2("SELECT key FROM tags WHERE person=? AND key<>?", Array(person, str))
M Mahares Expert Licensed User Longtime User Feb 15, 2023 #3 This is how I would do it: Code: Copy Dim str As String "Sergey" rs = SQL1.ExecQuery2("SELECT key FROM tags WHERE person= ? and key NOT LIKE ?", Array As Object(1, $"%${str}%"$)) Upvote 0
This is how I would do it: Code: Copy Dim str As String "Sergey" rs = SQL1.ExecQuery2("SELECT key FROM tags WHERE person= ? and key NOT LIKE ?", Array As Object(1, $"%${str}%"$))
Sergey_New Well-Known Member Licensed User Longtime User Feb 15, 2023 #4 Many thanks to JCO and Mahares! And using the operator EXISTS can be faster? Upvote 0
JCO Active Member Licensed User Longtime User Feb 15, 2023 #5 Just to clarify. My line would exclude only records where key is exactly str Mahares' line would exclude any record where the key contains str Depending on what you need, use one or the other Upvote 0
Just to clarify. My line would exclude only records where key is exactly str Mahares' line would exclude any record where the key contains str Depending on what you need, use one or the other
Sergey_New Well-Known Member Licensed User Longtime User Feb 15, 2023 #6 Need key exactly equal to str. Upvote 0
JCO Active Member Licensed User Longtime User Feb 15, 2023 #7 Sergey_New said: Many thanks to JCO and Mahares! And using the operator EXISTS can be faster? Click to expand... AFAIK, EXISTS should only be used when working with subqueries Upvote 0
Sergey_New said: Many thanks to JCO and Mahares! And using the operator EXISTS can be faster? Click to expand... AFAIK, EXISTS should only be used when working with subqueries
Sergey_New Well-Known Member Licensed User Longtime User Feb 15, 2023 #8 JCO said: used when working with subqueries Click to expand... I understand. Upvote 0
Sergey_New Well-Known Member Licensed User Longtime User Feb 15, 2023 #9 P.S. I would like the search to stop at the first mismatch Last edited: Feb 15, 2023 Upvote 0
M Mahares Expert Licensed User Longtime User Feb 15, 2023 #10 Sergey_New said: would like the search to stop at the first mismatch Click to expand... JCO said: Mahares' line would exclude any record where the key contains str Click to expand... Add LIMIT 1 at the end of the query. See below Code: Copy Dim str As String "Sergey" rs = SQL1.ExecQuery2("SELECT key FROM tags WHERE person= ? and key NOT LIKE ? LIMIT 1", Array As Object(1, $"%${str}%"$)) Please note that the code I posted also excludes the EXACT search and also besides anywhere in the column. If you use JCO's code the query will be: "SELECT key FROM tags WHERE person=? AND key<>? LIMIT 1" Upvote 0
Sergey_New said: would like the search to stop at the first mismatch Click to expand... JCO said: Mahares' line would exclude any record where the key contains str Click to expand... Add LIMIT 1 at the end of the query. See below Code: Copy Dim str As String "Sergey" rs = SQL1.ExecQuery2("SELECT key FROM tags WHERE person= ? and key NOT LIKE ? LIMIT 1", Array As Object(1, $"%${str}%"$)) Please note that the code I posted also excludes the EXACT search and also besides anywhere in the column. If you use JCO's code the query will be: "SELECT key FROM tags WHERE person=? AND key<>? LIMIT 1"
Sergey_New Well-Known Member Licensed User Longtime User Feb 15, 2023 #11 Mahares,This query returns the first row of the table on a mismatch, not an empty value. Upvote 0
M Mahares Expert Licensed User Longtime User Feb 15, 2023 #12 Sergey_New said: This query returns the first row of the table on a mismatch, not an empty value. Click to expand... If for instance, you have 5 that do not match, it will only show the first of the 5 records. That is how I understood the question. If not, please explain, preferably with an example. Added this: If you have keyx that have a empty string and you want to display it, as the first one, add this to the query: before LIMIT 1: ORDER BY keyx LIMIT 1 Last edited: Feb 15, 2023 Upvote 0
Sergey_New said: This query returns the first row of the table on a mismatch, not an empty value. Click to expand... If for instance, you have 5 that do not match, it will only show the first of the 5 records. That is how I understood the question. If not, please explain, preferably with an example. Added this: If you have keyx that have a empty string and you want to display it, as the first one, add this to the query: before LIMIT 1: ORDER BY keyx LIMIT 1
Sergey_New Well-Known Member Licensed User Longtime User Feb 15, 2023 #13 This is what I need. Figured it out: replaced "<>" with "=" B4X: Copy rs = sql1.ExecQuery2("SELECT key FROM tags WHERE person=? AND key=? LIMIT 1", Array(person, str)) Upvote 0
This is what I need. Figured it out: replaced "<>" with "=" B4X: Copy rs = sql1.ExecQuery2("SELECT key FROM tags WHERE person=? AND key=? LIMIT 1", Array(person, str))
M Mahares Expert Licensed User Longtime User Feb 16, 2023 #14 Sergey_New said: And using the operator EXISTS can be faster? Click to expand... There are ways to improve performance of queries. If you have one DB that is giving you trouble in terms of performance and you are receptive to post it or its project and show what slows it down, I am sure members will take a stab at it and report back to you. Upvote 0
Sergey_New said: And using the operator EXISTS can be faster? Click to expand... There are ways to improve performance of queries. If you have one DB that is giving you trouble in terms of performance and you are receptive to post it or its project and show what slows it down, I am sure members will take a stab at it and report back to you.
Sergey_New Well-Known Member Licensed User Longtime User Feb 16, 2023 #15 So far, I do not have an urgent need to improve performance. Wanted to get the best solution. Mahares, thank you for in resolving my issues! Upvote 0
So far, I do not have an urgent need to improve performance. Wanted to get the best solution. Mahares, thank you for in resolving my issues!