Quickbooks and pyodbc LIKE pattern matching - pyodbc

I'm using pyodbc to run some queries against Quickbooks and having some issues with pattern matching, specifically with square bracket wildcards. I don't even know if I'm allowed to use square bracket wildcards or where I would go to find that answer. The pyodbc docs don't show any examples using them from what I can see. The only wildcard that seems to work that I've tried is %.
I'm trying to build a pattern to match on these two strings:
M-100
J-100
This is the query that is returning no rows:
cursor.execute("SELECT ListID,FullName from ItemInventoryAssembly WHERE Name LIKE ?", '[MJ]-100')
These queries do return rows:
cursor.execute("SELECT ListID,FullName from ItemInventoryAssembly WHERE Name LIKE ?", 'M-100')
cursor.execute("SELECT ListID,FullName from ItemInventoryAssembly WHERE Name LIKE ?", 'J-100')
Do I have to use the following instead of [] wildcards? (I have many more names to match on and this query would end up being very long and ugly.)
cursor.execute("SELECT ListID,FullName from ItemInventoryAssembly WHERE Name LIKE ? OR Name LIKE ?", ('M-100','J-100'))
Additionally, where would I go to find which wildcards I'm allowed to use in my setup with pyodbc and Quickbooks?

Related

Unary NOT in SQLite FTS5 MATCH query

The SQLite FTS5 docs say that search queries such as SELECT ... WHERE MATCH '<query1> NOT <query2>' are supported, but it looks like there's no support for the unary NOT operator.
For example, if I want to search for everything that doesn't match <query>, I cannot use MATCH 'NOT <query>'. I would have to use NOT MATCH '<query>', which is a completely different thing (the FTS5 module never gets to see the NOT operator, as it is outside the quotation marks). Only the text inside the quotation marks is the search query.
I need to find a way to use an unary NOT operator inside the search query. I can't use it outside, because I only get to control the search query text, and not the rest of the SQL statement.
A possible approach I've thought of would be to find a search query that matches anything, and do MATCH '<match_anything> NOT <query>'. However, I've found no way to match everything in a search query.
Can you think of a way to have the behaviour of the unary NOT operator inside the search query?
Try this ..
SELECT * FROM docs
WHERE ROWID NOT IN (
SELECT ROWID FROM docs WHERE content MATCH '<query>'
)

How to query in Azure cosmos db where query parameter has a single qutoe

I have a query is azure cosmos db
SELECT * FROM c where c.productConfigId in ('Levi's')
since Levi's contains a single quote, the query is breaking because of the comma. I checked the azure docs and they recommend to use \' to escape but that only seems to work when you have no trailer letter after the comma. Anyone knows the possible solution to this?
I was able to successfully run the query by escaping ' with \'. Here's the query I ran:
SELECT * FROM Root r where r.Content in ('Levi\'s Jeans')
Other alternative is to use double quotes ("). Something like:
SELECT * FROM Root r where r.Content in ("Levi's Jeans")

sqlite3 fts3 multiple columns search including special characters

i am using sqlite3 fts3. (sqlite3 version is 3.7.17)
I tried to search keywords including special characters (ex. #, ?) in multiple columns.
This is my examples.
SELECT * FROM table_name WHERE table_name MATCH
'EMAIL:aaa#test.com OR SUBJECT:is it a question?'
This query have to return a result having email address is 'aaa#test.com' or subject is 'is it a question?'
But this query is not return correct results.
I think that sqlite3 fts3 can't recognize special characters...
How can i solve this problem? :(
To do a phrase query, you must use quotes.
Special characters are filtered out by the default tokenizer; aaa#test.com must be handled as a phrase with three words.

SQlite3 bound params and LIKE

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 '%' || ?

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)

Resources