i`m trying to search a Sqlite Database , with this condition : i want to find a string using an Exact Keyword. let me explain this to you .
i have 3 rows as follow :
catching cold
i have a cat
two cats was seen in your house yesterday
i want to search these rows with keyword "cat" and i expect this result :
i have a cat
i am using this SQL code so far :
Select * FROM MyTable WHERE Mycolumn Like '%cat%'
But Returning Result is All these 3 Rows:
catching cold
i have a cat
two cats was seen in your house yesterday
What can i do to get my expected result?
thank you in advance.
The % character in the argument of a LIKE clause matches any string, including the empty string. Unfortunately, SQLite doesn't have the REGEXP function built in (and Android's SQLite doesn't have it).
What you can do instead is use FTS (full text search). How to do so is described here: https://www.sqlite.org/fts3.html#section_1_2
Using your example, you would set it up like so:
create virtual table textsearch using fts4(content);
insert into textsearch (content) values ('catching cold'), ('i have a cat'), ('two cats was seen in your house yesterday')
Then you can do a simple text query with the MATCH operator:
select * from textsearch where content match 'cat';
If you try the above in a sqlite3 shell, you'll see it returns only 'i have a cat'. There's a lot more you can do with the match operator, explained on the page I linked above.
You can use a regular expression with a special pattern for word boundaries.
Select * FROM MyTable WHERE Mycolumn = 'cat'
Corrected my answer i think that should work.
Related
I try to make query from table with search by first and last names columns.
When input simple name with 1 word (like "Tom" ) all fine. Query with 2 or more search keywords not works. F.e "Anny Lee". Is there possible way to search by concatenated contacts_table.first_name and contacts_table.last_name value in DB table? The part of query below:
AND ( (contacts_table.first_name || contacts_table.last_name LIKE '%KEYWORD%') OR (....))
Cant implement OR statement in right way.
I have a list of names of starts with characters and end with numbers like: -
ka1, ka10, ka 2, ka, sa2, sa1, sa10, p1a10, 1kb, p1a2, p1a11, p1a.
I want to sort it in natural order, that is: -
1kb, ka, ka1, ka 2, ka10, p1a, p1a2, p1a10, p1a11, sa1, sa2, sa10.
The main problem I am seeing here is no delimiter between text and numeric part, there also a chance of without numeric part also.
I am using sqlite in android, I can do sorting using java after fetching points by cacheing cursor data, but I am using(recommended to use) cursor adapter.
Please suggest a query for sorting or is there any way to apply sorting in cursor?
I tried below query for Natural sorting:
SELECT
item_no
FROM
items
ORDER BY
LENGTH(item_no), item_no;
It worked for me in Sqlite db too. Please see this link, for more details.
I can propose using regex replacement adding zeros, creating temporary table of original and corresponding values, then follow this link for sorting it: http://www.saltycrane.com/blog/2007/12/how-to-sort-table-by-columns-in-python/
tip for regex add as many zeros after last letter, but limit the number of total digits for predicted maximum number of digits. If You need help with regex as well, provide exact info of valid and invalid values, so can halp with that too.
PS if want to be sure that zeros goes before last digits search for char from the end
Updated
You can use different ways - Some of are mentioned below:
BIN Way
SELECT
tbl_column,
BIN(tbl_column) AS binray_not_needed_column
FROM db_table
ORDER BY binray_not_needed_column ASC , tbl_column ASC
Cast Way
SELECT
tbl_column,
CAST(tbl_column as SIGNED) AS casted_column
FROM db_table
ORDER BY casted_column ASC , tbl_column ASC
or try the solution:
There are a whole lot of solutions out there if you hit up Google, and
you can, of course, just use the natsort() function in PHP, but it's
simple enough to accomplish natural sorting in MySQL: sort by length
first, then the column value.
Query: SELECT alphanumeric, integer FROM sorting_test ORDER BY LENGTH(alphanumeric), alphanumeric from here
I am using FTS for querying my database for increasing the search speed as I need to search in text description also,
When I am trying to query using single column its working fine like below
select * from productsearch where productsearch match ('prod_name:panasonic*tw*')
And also,
select * from productsearch where productsearch match ('prod_short_desc:samsung*s5*')
So, above both queries give me expected result but when I try to combine both queries using OR operator its not giving me any result
select * from productsearch where productsearch match ('prod_name:panasonic*tw* OR
prod_short_desc:samsung*s5*')
So, I want to know if I am doing anything wrong in here while using OR operator for searching multiple columns
UPDATE
Below query is working fine but that doesn't fulfill my requirement,
select * from productsearch where productsearch match ('prod_name:panasonic* OR
prod_short_desc:samsung*')
You can see that if I remove the multiple tokens then its working fine with OR operator too.
SQLite's FTS supports only simple prefix searches.
A query like prod_short_desc:samsung*s5* consists of two expressions, prod_short_desc:samsung* and s5*, which behave exactly the same as if you had written prod_short_desc:samsung* s5*.
If you have compiled SQLite for the enhanced query syntax, you could use a query like:
prod_short_desc:samsung* prod_short_desc:s5* OR prod_name:panasonic* prod_name:tw*
If you have compiled SQLite for the standard query syntax, you cannot use a single query for this because the OR operator's precedence is too high and cannot be modified with parentheses.
So, I found the solution finally,
instead of searching from all the columns individually, I created a single column in the database which contains data for required columns to be searched into,
Example
I need to search in prod_name & prod_short_desc columns, so I created a column named data in database and appended the values of prod_name & prod_short_desc then looking up into only data field worked like a charm
prod_name | prod_short_desc
samsung | samsung s5
So, now I merged the data of both the columns into one with space as a seperator
data
samsung samsung s5
And then search was very fast indeed with below query,
select * from productsearch where productsearch match ('samsung*s5*')
I have a Sqlite3 database in android, with data are sentences like: "good afternoon" or "have a nice day", now I want to have a search box, to search between them, I use something like this :
Cursor cursor = sqliteDB.rawQuery("SELECT id FROM category WHERE sentences LIKE '"+ s.toString().toLowerCase()+ "%' LIMIT 10", null);
But it only show "good afternoon" as result if user start searching with first "g" or "go" or "goo" or etc, how can I retrieve "good afternoon" as results, if user search like "a" or "af" or "afternoon".
I mean I want to show "good afternoon" result, if user search from middle of a data in sqlite3 db, not only if user searches from beginning.
thanks!
Just put the percent sign in front of your query string: LIKE '%afternoon%'. However, your approach has two flaws:
It is susceptible to SQL injection attacks because you just insert unfiltered user input into your SQL query string. Use the query parameter syntax instead by re-writing your query as follows:
SELECT id FROM category WHERE sentences LIKE ? LIMIT 10. Add the user input string as selection argument to your query method call
It will be dead slow the bigger your database grows because LIKE queries are not optimized for quick string matching and lookups.
In order to solve number 2 you should use SQLite's FTS3 extension which greatly speeds up any text-related searches. Instead of LIKE you would be using the MATCH operator that uses a different query syntax:
SELECT id FROM category WHERE sentences MATCH 'afternoon' LIMIT 10
As you can see the MATCH operator does not need percent signs. It just tries to find any occurrence of a word in the whole text that is being searched (in your case the sentences column). Read through the documentation of FTS3 I've linked to. The MATCH query syntax provides some more pretty handy and powerful options for finding text in your database table which are pretty similar to early search engine query syntax such as:
MATCH 'afternoon OR evening'
The only (minor) downside to the FTS3 extension is that it blows up the database file size by creating additional search index tables and meta-data. But I think it's well worth it for this use case.
Let's say an SQLite database column contains the following value:
U Walther-Schreiber-Platz
Using a query I'd like to find this record, or any similar records, if the user searches for the following strings:
walther schreiber
u walther
walther-schreiber platz
[Any other similar strings]
However I cannot figure out any query which would do that. Especially if the user does not enter the - character.
Example:
select * from myTable where value like '%walther schreiber%'; would not return the result because of the missing -.
Thanks,
Robert
So, as I said in my comment, I think you can have a query along the lines of:
select * from myTable where LOWER(value) like <SearchValue>
I'm assuming you're collecting the <SearchValue> from the user programmatically, so would be able to do the following: <SearchValue> would need to be: The user's search string, appropriately cleansed to avoid SQL injection attacks, converted to lower case, with all of the spaces converted to '%', so that they match zero or more characters...
So you would have (for example):
select * from myTable where LOWER(value) like '%walther%schreiber%'
select * from myTable where LOWER(value) like '%walther-schreiber%platz%'
etc... however, this does assume that the word order is the same, which in your examples it is...