sqlite, how replace a value from CSV saved in a column - android

I have a table in my android app with a column named tags, this column can contain one or multiple comma separated values, for example :
test1,test,test2
now I want to rename one of these values in entire table, I have the following query :
update mytable
set
tags = replace (tags, ',test,',',XXX,') // rename test with XXX
where
tags like '%,test,%'
this works fine as far as the value is in between commas.
How can I achieve this for the situation where my value is at the start or end of the csv or is the only value in csv.

You need to append commas at the start and at the end of the column and operate on that:
update mytable
set
tags = trim(replace(',' || tags || ',', ',test,', ',XXX,'), ',')
where
',' || tags || ',' like '%,test,%';
See the demo

Related

Sqlite query to get list of next available characters after the matching search keyword

How to query sqlite database to get list of next available characters after the search keyword in a string field.
for example, if the search keyword is ’and’
and let the strings be a list of names like :
Andy Xyz
Andrew Xyz
Xyz Andon
Xyz Miranda
then i should get the characters [y,r,o,a].
I tried the query with substr(names,1,1), but substr needs to specify the start index which will be different in each strings. Is it possible to fetch these characters using sqlite query?
You need substr() and instr():
select
substr(names, instr(lower(names), lower('and')) + length('and'), 1) nextchar
from tablename
where
names like '%' || 'and' || '_%'
See the demo
You can replace 'and' with any string you wish in the above query.
You could utilise the instr function to ascertain the start of the substr e.g. :-
SELECT *, substr(names,instr(lower(names),'and') + length('and'),1) AS onechar
FROM mytable WHERE names LIKE ('%and%');
A working example :-
DROP TABLE IF EXISTS mytable;
CREATE TABLE IF NOT EXISTS mytable (names TEXT);
INSERT INTO mytable VALUES('Andy Xyz'),('Andrew Xyz'),
('Xyz Andon'),('Xyz Miranda');
SELECT *, substr(names,instr(lower(names),'and') + length('and'),1) AS onechar FROM mytable WHERE names LIKE ('%and%');
This results in :-
as per your expected results.

Is it possible to use SQLite pattern matching on a result set?

Is there a way I can apply the LIKE clause to a result set in SQLite? What I would like to accomplish is the following:
SELECT *
FROM MyTable
WHERE value LIKE
(
SELECT CASE
WHEN prefix IS NOT NULL THEN prefix || '?'
ELSE code END
FROM MyOtherTable
)
Adding the ? to the end of does not allow me to wildcard search by the prefix and the LIKE just ends up functioning like an IN clause.
Is there some way to achieve this, or alternatively a better way to achieve the same result?
Your primary issue is that ? is not a pattern matching character, rather that _ is for pattern matching any single character and % is for pattern matching a sequence of characters.
SQL As Understood By SQLite - expression - The LIKE, GLOB, REGEXP, and MATCH operators
However, you would also encounter issues if myOtherTable has multiple rows.
Consider the following :-
DROP TABLE IF EXISTS MyTable;
DROP TABLE IF EXISTS MyOtherTable;
CREATE TABLE IF NOT EXISTS MyTable (value TEXT);
CREATE TABLE IF NOT EXISTS MyOtherTable (prefix TEXT, code TEXT);
INSERT INTO MyOtherTable VALUES
(null,'mycode'),
('A','myothercode'),
('S',null)
;
INSERT INTO MyTable VALUES
('Something'), -- matched by S%
('A something'), -- matched by A%
('mycode is this'), -- never matched as characters after mycode
('Another'), -- matched by A%
('mycode') -- matched by mycode
;
-- Intermediate
SELECT CASE
WHEN prefix IS NOT NULL THEN prefix || '%'
ELSE code END
FROM MyOtherTable
;
SELECT *
FROM MyTable
WHERE value LIKE
(
SELECT CASE
WHEN prefix IS NOT NULL THEN prefix || '%'
ELSE code END
FROM MyOtherTable
);
The final result would be :-
A single row
mycode
That is only the first result of the subquery is considered.
If (null,'mycode'), is removed or commented out then the result would be :-
Two rows that match the now first A% i.e.
A something
Another
Removing or commenting out the first two (null,'mycode'), and ('A','myothercode'), results in the single row :-
Something

Sqlite query to find all rows where a particular column field value lies in input string

I got a scenario where I have to find all rows in Android SQLite db, where a particular column field value lies in the input string. For example,
Input string:
I am not done yet, I can’t call it a day I need to work more for couple of hours.
Now my table in SQLite(Android) has a column called Title
I need to find all rows whose Title column field values lies in above input string. Basically I want following rows
where the full Title string is like one of below
call it a day
am not done yet
I want to know if it is possible using a single query in sqlite Android or not. What I know is LIKE query work on opposite manner.
Given that your table contains a column title which has titles that you want to match in the given string, you can do concatenation with % to use LIKE
select *
from your_table
where 'your input string here' like '%' || title || '%'

SQLite matching column name , ignoring commas

I've got a database with several columns and a column name and I'm doing the following sqlite statement through OrmLite:
SELECT `name`,`id`,`ita_name`,`setCode`
FROM `MyTable`
WHERE (`name` LIKE ?)
ORDER BY `name` LIMIT 30
everything works fine but if the value of the name column is "Michael*,* Basketball player" and I submit "Michael Basketball player" the query return no results.
I've already tried to use %myQueryText% but with no success.
What can i do to return values from my column ignoring "," ?
SQL LIKE statement is more specific, than you think. The % wildcard matches zero or more characters at the position of the wildcard, so %myQueryText% matches anythingmyQueryText OR myQueryTextanything but not myQuery(comma)text. You need place wildcard at position of comma, see SQL LIKE wildcards.
So you probably want something like
... WHERE (`name` LIKE "Michael%Basketball player") ...
Edit/ Prepare your search pattern in Java, possibly like
String searchPattern = "Michael Basket player";
String preparedPattern = searchPattern.replace(' ', '%');
// outputs pattern "Michael%Basket%player"
Which could match even Michael the best Basketbal and other sports player and if you want to overcome such troubles, you can use REGEXP and prepare search pattern:
String searchPattern = "Michael Basket player";
String preparedPattern = searchPattern.replace(' ', '[,\s]+');
// outputs pattern "Michael[,\s]+Basket[,\s]+player"
and then in SQL query change WHERE statement to
WHERE `name` REGEXP "Michael[,\s]+Basket[,\s]+player"
the [,\s]+ matches either comma or white space and the occurance is one or multiple times, so it matches one space or space and comma or comma and more spaces...

Search String in column value of Sqlite

I am trying to search a String in column of my table .The values are stored in
column in this format
My Search String is similar as above. i.e: comma separated String
My query is to find out whether any value in search String is present is my column or not.
Things I have Tried:
using Like : Like does it.But it matches individual values i.e for 3 search string values I have to make 15 like condition(costly for large table)
SELECT * from A WHERE mycol LIKE 'myval,%' or mycol LIKE '%, myval,%' or mycol or LIKE '%, myval'
using instr function : instr matches the String but only the First Occurance.
e.g: select * from A where instr ('123' ,'12')
using In : This donot search within the String .But Matches the values individually.
I have tried Like function Like(X , Y) from docs Here
So my Question is there a way , instead of Individually searching my search string in the column using Like Operator or In function . I could search my values values in single query something like combination of In and Any(not supported in Sqlite) function does in other Db
i.e select * from A where Any('my search string') in ('my column value')
Any Answer or comment is HIGHLY Appreciated.Hoping for reply.
You have problems because your database is not properly normalized.
To have multiple search IDs for each MyTable row, create a second table that can have multiple rows, with one search ID in each row:
CREATE TABLE MyTable (
MyTableID INTEGER PRIMARY KEY,
[...]
);
CREATE TABLE MyTableSearchIDs (
MyTableID INTEGER REFERENCES MyTable(MyTableID),
SearchID INTEGER -- or TEXT or whatever
);
To search for a MyTable row, search its ID first:
SELECT *
FROM MyTable
WHERE MyTableID IN (SELECT MyTableID
FROM MyTableSearchIDs
WHERE SearchID = 'myvalue')
The same can be done with a join:
SELECT MyTable.*
FROM MyTable
JOIN MyTableSearchIDs USING (MyTableID)
WHERE MyTableSearchIDs.SearchID = 'myvalue'

Categories

Resources