SQLITE3 Error: "SQLITE_RANGE: column index out of range" - sql

I'm trying to search for a user by a string so kind of like autocompletion
Here's my SQL:
SELECT * FROM users WHERE username LIKE "%?%" LIMIT 25;
And it gives me this error:
SQLITE_RANGE: column index out of range
I looked into a bunch of github issues but couldn't find any that works for me.
In the SQLite CLI it works, so I have no idea why it wouldn't work here.

A ? inside a string like "%?%" is not considered as a placeholder for the parameter that you pass, which throws the error that you get because the parameter has no where to be placed in the statement.
Use concatenation of the ? with the wildcards:
SELECT * FROM users WHERE username LIKE "%" || ? || "%" LIMIT 25;

Related

Getting "MALFORMED_QUERY: unexpected token: *" when trying to bulk query in salesforce

I'm trying to work around Salesforce's limit of 2000 documents per query and found a solution:
# simple_salesforce set up as sf
query = "SELECT * FROM Opportunity LIMIT 10"
fetch_results = sf.bulk.Opportunity.query(query, lazy_operation=True)
all_results = []
for list_results in fetch_results:
all_results.extend(list_results)
For which I get the error message simple_salesforce.exceptions.SalesforceGeneralError: Error Code Failed. Response content: InvalidBatch : Failed to process query: MALFORMED_QUERY: unexpected token: *
What's weird is that I'm getting a normal response when not using * e.g. when query = "SELECT * FROM Opportunity LIMIT 10".
I need all fields in my query - does anyone know how to select all fields? I've tried googling the issue but seeing as * is a google functional characted and I've not found a way to escape it in a search this has been very difficult. Any help is appreciated!
Found the correct syntax in case anyone else had this problem: FIELDS(ALL) is the correct substitute for * so we have query = "SELECT FIELDS(ALL) FROM Opportunity LIMIT 10"

Dynamically change project used in a SQL query

I am using BigQuery, Standard SQL, and I want to dynamically change parts of the FROM clause, such as the project id. I have been looking for a solution for this the last 3 years - the problem has been that parameters cannot be used as inputs in the FROM clause. The benefit would be to create a stored procedure, where the project id can be passed in as an argument and can query the appropriate project. The projects would have the same datasets and table names - this would be our way of building a Master query for easy development and implementation. Instead of changing 15 clients' views, we can change the Stored Procedure once and it will push out the changes to all clients' views. However, I have always gotten hung up on dynamically changing the FROM clause!
For example:
DECLARE ProjectId STRING DEFAULT 'test_project';
SELECT col_1 FROM `#ProjectId.Dataset.Table`;
would always error out due to parameters not being able to be used in the FROM clause. However, I saw a related post on using dynamic SQL to overcome this obstacle. I've been looking into the EXECUTE IMMEDIATE function within BigQuery, as this is what has been cited to be a solution. From that post I attempted to implement in several ways:
Attempt #1:
DECLARE ProjectId STRING DEFAULT 'test_project';
EXECUTE IMMEDIATE CONCAT(
"SELECT * FROM ", #ProjectId, ".DataSet.`Table` " )
^ This gives an error "Query error: Undeclared query parameters at [2:19]"
Attempt #2:
EXECUTE IMMEDIATE CONCAT(
"SELECT * FROM ", #ProjectId, ".DataSet.`Table` " )
USING 'my-project' as ProjectId, 'my-dataset' as DataSet;
^ which gives the error "Query error: Undeclared query parameters at [1:19]"
Third and final attempt was to try declaring the parameter within the EXECUTE IMMEDIATE:
EXECUTE IMMEDIATE CONCAT(
"DECLARE ProjectId STRING DEFAULT 'test_project'; ",
"SELECT * FROM ", #ProjectId, ".DataSet.`Table` " )
USING 'my-project' as ProjectId, 'my-dataset' as DataSet;
^ which, you guessed it, results in the same error "Query error: Undeclared query parameters at [1:19]"
I am reaching out to see if anybody has had success with this? I see the value in the Dynamic SQL statements, and have read the documentation and some examples, but it still doesn't seem to work when trying to dynamically change the FROM clause. Any help is much appreciated, willing to try whatever is thrown out - excited to learn what can be done!
Just remove #:
DECLARE ProjectId STRING DEFAULT 'test_project';
EXECUTE IMMEDIATE CONCAT(
"SELECT * FROM ", ProjectId, ".DataSet.`Table` " )

Get data from sqlite database using LIKE cmd %pattern% works but '%'+pattern+'%' didnt work

I want to search and filter the sqlite database based on a input provided by user using SQL like command. %pattern% works fine, but %+pattern+% format didnt work, Anyother way of using the same?
I have tried the different forms of the query in DB browser for sqlite.. Didnt find luck..
Code:
return this.database.executeSql("SELECT * FROM table where name LIKE '%'+im+'%'",[] )
Expected to see the same output as %im% when using '%'+im+'%' and "im" will be replaced by user's input
You need to use || concatenate strings:
SELECT * FROM table WHERE name LIKE '%' || im || '%'
db<>fiddle demo

JPA native query gives incorrect output

I know this may sound silly but I've been stuck on this problem for too long!
I'm querying a PostgreSQL repository through JPA using native SQL queries. One of my queries looks like this:
#Query(value = "select * from gs where ?1 = ?2", nativeQuery = true)
public List<GsJsonStore> matchJson(String term, String value);
I'm testing the function using :
List<GsJsonStore> list = repo.matchJson("subject", "'Sub'");
The list is empty on running the query, however when I run the same query through PSQL command line using:
select * from gs where subject = 'Sub';
I get the correct output, records contatining the key-value pair are returned.
Where am I making the mistake?
You can't use parameter for column name. Your query resolves to
select * from gs where 'subject' = '''Sub'''
EDIT: just saw #pozs already posted the same in comment

lucene query issue

I am using Lucene with Alfresco. Here is my query:
( TYPE:"{com.company.customised.content.model}test" && (#\{com.company.customised.content.model\}testNo:111 && (#\{com.company.customised.content.model\}skill:or))
I have to search documents which are having property skill of value "or". The above query is not giving any results (I am getting failed to parse query).
If I use the query up until testNo (ignoring skill), I am getting proper results:
( TYPE:"{com.company.customised.content.model}test" && (#\{com.company.customised.content.model\}testNo:111))
Can you please help me?
Thanks
Unfortunately, "or" is a reserved keyword in Lucene. Therefore, Lucene fails to properly interpret your query, because Lucene thinks you are referring to the OR boolean operator. You may want to try wrapping the or in double quotes:
( TYPE:"{com.company.customised.content.model}test" && (#\{com.company.customised.content.model\}testNo:111 && (#\{com.company.customised.content.model\}skill:"or"))
I am not familiar with Alfresco, so you may not be able to do this.
Yes , or is reserved keyword in lucene but if you are trying to query by property which is of type number then you can give your value directly else if of type d:text or string so that you have to gave your value in double quote " " .
( TYPE:"{com.company.customised.content.model}test" && (#{com.company.customised.content.model}testNo:111 && (#{com.company.customised.content.model}skill:"or"))