SQlite3 bound params and LIKE - sqlite

Sqlite3 provides the sqlite3_bind_* functions which allow one to do parameter substitution into a SQL query. My question is: what is the right way to combine this with LIKE queries? For example, I might want to do:
SELECT * FROM thing WHERE name LIKE '%?'
but that doesn't work at all. Is the best way really just:
SELECT * FROM thing WHERE name LIKE ?
and then put the pattern characters into the actual string value to be substituted?

To concatenate strings, use the || operator:
SELECT * FROM thing WHERE name LIKE '%' || ?

Related

Finding rows that start with a and b in SQLITE?

Im trying to find all rows that start with J or M in sqlite. For example in Microsoft Access I know you can use :
SELECT *
FROM Customers
WHERE cust_name LIKE '[JM]%';
However Im not sure what the equivalent of [] is in sqlite. I have
tried:
SELECT *
FROM Customers
WHERE cust_name LIKE 'J%' OR 'M%'
ORDER BY cust_contact;
But this only returns rows that start with J :/ Any help would be appreciated.
Or use GLOB instead of LIKE, but it is case sensitive and uses a different syntax.
WHERE cust_name GLOB '[JjMm]*'
LIKE is case sensitive unless options have been set otherwise, like PRAGMA case_sensitive_like = boolean;. Although case sensitivity can be set for LIKE, GLOB is always case sensitive.
Try the following code
SELECT * FROM Customers
WHERE cust_name LIKE 'J%' OR
cust_name LIKE 'M%'
ORDER BY cust_contact;

R sqlexecute wildcard

Using RODBCext (and Teradata) my SQL query often need to be restricted and is done so with a where statement. However, this is not always required and it would be beneficial to not restrict, but I would like to use a single SQL query. (The actual query is more complex and has several instances of what I'm attempting to apply here)
In order to return all rows, using a wildcard seems like the next best option, but nothing appears to work correctly. For example, the sql query is:
SELECT *
FROM MY_DB.MY_TABLE
WHERE PROC_TYPE = ?
The following does work when passing in a string for proc_type:
sqlExecute(connHandle, getSQL(SQL_script_path), proc_type, fetch = TRUE)
In order to essentially bypass this filter, I would like to pass a wildcard so all records are returned.
I've tried proc_type set to '%', '*'. Also escaped both with backslashes and enclosed with double-quotes, but no rows are ever returned, nor are any errors produced.
You could use COALESCE to do this:
SELECT *
FROM MY_DB.MY_TABLE
WHERE PROC_TYPE = COALESCE(?, PROC_TYPE);
In the event that your parameter is NULL it will choose PROC_TYPE to compare to PROC_TYPE which will return everything.
As for your wildcard attempt you would have to switch over to an operator that can use a wildcard. Instead of =, LIKE for instance. I think you would end up with some oddball edge cases though depending on your searchterm and the data in that column, so the COALESCE() option is a better way to go.

How to perform SQLite LIKE queries with wildcards not read as wildcards

I'm running into a problem in SQLite when querying on text fields that happen to have the _ or % wildcard characters.
I have a table with a 'Name' field I want to query on. Two of my records have the value 'test' and 'te_t' in the 'Name' field I want to query on. If I run a query like below
"SELECT ALL * from Table WHERE Name LIKE 'te_t'"
This will return both the 'te_t' and 'test' records, because of '_' being read as a wildcard. How do I make it so that I only get the 'te_t' record from the above query?
I've done some research on this and read that I should be able to throw a backslash '\' character in front of the wildcard to get it to be read as a normal _ character instead of a wildcard. But when I try the query
"SELECT ALL * from Table WHERE Name LIKE 'te\_t'"
my query returns zero matches.
What am I doing wrong? Is this just not possible in SQLite?
In SQL, you can escape special characters in the LIKE pattern if you declare some escape character with ESCAPE:
SELECT * FROM MyTable WHERE Name LIKE 'te\_t' ESCAPE '\'
(see the documentation)

sqlite full text wild card search

Can Sqlite FT3 or FT4 do something like
SELECT * FROM MyTable WHERE body MATCH '*qlite'
I know this:
SELECT * FROM MyTable WHERE body MATCH 'Sqlite*'
works but seems like '%like' like operation doesn't work in the full text.
From what I understand it's a limitation of FTS in general, across platforms, that suffix/postfix searches aren't possible.
The best workaround I've seen is to add a column to MyTable called ReverseBody and store the reverse of the Body column in there and add it to the FT index as well. Then you write queries like
select * from MyTable where reversebody match (REVERSE('qlite') + '*')
I work in SQL Server so we have a REVERSE built in. I don't think SQLite does, but you can add custom functions to do it as descrbed here

sqlite Query optimisation

The query
SELECT * FROM Table WHERE Path LIKE 'geo-Africa-Egypt-%'
can be optimized as:
SELECT * FROM Table WHERE Path >= 'geo-Africa-Egypt-' AND Path < 'geo-Africa-Egypt-zzz'
But how can be this done:
select * from foodDb where Food LIKE '%apples%";
how this can be optimized?
One option is redundant data. If you're querying a lot for some fixed set of strings occuring in the middle of some column, add another column that contains the information whether a particular string can be found in the other column.
Another option, for arbitrary but still tokenizable strings is to create a dictionary table where you have the tokens (e.g. apples) and foreign key references to the actual table where the token occurs.
In general, sqlite is by design not very good at full text searches.
It would surprise me if it was faster, but you could try GLOB instead of LIKE and compare;
SELECT * FROM foodDb WHERE Food GLOB '*apples*';

Resources