FMDB Database Query IOS FIND_IN_SET() - ios

I am using FMDB database in IOS. In my database I have a one column called tags.
Tags column have a comma separated Value like apple,banana,. I want Title column where my keyword matches. I want to know how to search comma separated value in table column Please help.

You need to use LIKE operator, when you have a field data like apple,banana,... you can add a , at the beginning and end of it like ,apple,banana,..., that makes all tags between two commas. Now you can check it if it contains ,yourTag,.
I'm not so familiar with FMDB, But I think below code can help you:
FMResultSet *rs = [db executeQuery:#"SELECT Title FROM yourTable WHERE ','+tags+',' LIKE ?",
[NSString stringWithFormat:#"%%%#%%", #"," + search_text + #","]];

Related

Rails query to substitute field and query

could someone please help me to write a query to substitute hyphens in rails DB field with space?
For eg:
If I have a field called 'name' in a table User having a value 'asdc-sd bc', and want to remove special characters like '-' and replace it with space to match with a given name 'asdc sd bc'.
I tried using lower to convert the name to lowercase but am not able to find out how to substitute the hyphens with space. Please help!
You can use SQL REPLACE function to replace - with spaces(' ').
For example, if you are querying on name column of User model, you can write your query like below:
query_str = 'asdc sd bc'
User.where("REPLACE(name, '-', ' ') = ?", query_str)

Issue regarding fetching only those records where field value contains only one occurrence of a particular character in rails

I need to write a rails active record where clause where I have to fetch those rows where name (name is a column in my table) contains only one occurrence of the character '.'
For example, if there is two rows in the table where name is "a.b" and "a.b.c", then my query should return the row having name "a.b" only.
Please help me to solve this.
Thanks in advance!
You can for example remove the dots and compare length.
SELECT * FROM table WHERE (char_length(name) - char_length(replace(name, '.', '')))=1
This is not very efficient though, because indexes can't be utilized.
To make things smoother, you could store the number of dots (depth?) in its own column with an index and query based on that. This could be done in insert/update trigger or in application layer, whatever suits your situation.
dbfiddle
with regexp ? you can try this :
select * from "table" where "name" ~ '^[^\.]*\.[^\.]*$'

Rails NOT IN query and regexp

I have array of strings:
a = ['*#foo.com', '*#bar.com', '*#baz.com']
I would like to query my model so I will get all the records where email isn't in any of above domains.
I could do:
Model.where.not(email: a)
If the list would be a list of strings but the list is more of a regexp.
It depends on your database adapter. You will probably be able to use raw SQL to write this type of query. For example in postgres you could do:
Model.where("email NOT SIMILAR TO '%#foo.com'")
I'm not saying thats exactly how you should be doing it but it's worth looking up your database's query language and see if anything matches your needs.
In your example you would have to join together your matchers as a single string and interpolate it into the query.
a = ['%#foo.com', '%#bar.com', '%#baz.com']
Model.where("email NOT SIMILAR TO ?", a.join("|"))
Use this code:
a = ['%#foo.com', '%#bar.com', '%#baz.com']
Model.where.not("email like ?",a.join("|"))
Replace * to % in array.

Batch insert values to a specific column in sqlite3 on iOS

I have a table in my database. I want to add values to a specific column in the table. Since there are like thousands plus rows in my table i figured i have to use batch update. The code is this but i get syntax error and a message saying there is no such row:
NSString *addProductColQuery = #"ALTER TABLE Sale ADD COLUMN Product text";
[self.dbManager loadDataFromDB:addProductColQuery];
NSString *batchstart = #"BEGIN IMMEDIATE TRANSACTION";
[self.dbManager loadDataFromDB:batchstart];
for (int i = 0; i<self.productInfo.count; i++)
{
NSString *addValue = [NSString stringWithFormat:#"INSERT INTO Sale (Product) VALUES (%#)",[tempProductArray objectAtIndex:i]];
[self.dbManager loadDataFromDB:addValue];
}
NSString *batchComit = #"COMMIT TRANSACTION";
[self.dbManager loadDataFromDB:batchComit];
Update:
I have managed to get the above code to work but now i am getting 90% CPU Usage on iphone6! I actually saw this coming since im using a for loop which is dead wrong to loop a query. Is there a way to batch insert the values to the rows in a specific column?
Are the values in tempProductArray text values? If so, you have to quote them in your SQL:
NSString *addValue = [NSString stringWithFormat:#"INSERT INTO Sale (Product) VALUES ('%#')", tempProductArray[i]];
Having shown you a quick solution, that's actually the wrong way to do it. It is inadvisable to use stringWithFormat to build SQL with text values. If any of those strings themselves have apostrophe characters in them, the SQL will fail. Also, if any of this data was provided by the end-user or from the network, you're exposed to SQL injection attacks.
The correct solution is to use sqlite3_prepare_v2 of the SQL without quotes, but with ? placeholder:
NSString *addValue = #"INSERT INTO Sale (Product) VALUES (?)";
And then, before calling sqlite3_step, you would call sqlite3_bind_text to bind a text value to the ? placeholder.
If your dbManager doesn't offer that capability, you'll have to add it. Or use a library, like FMDB, that offers this capability out of the box.

Symfony, propel, question mark

I want to create a search function on my website, and I don't want to use a plugin for this thing, because it's very simple, but I can't solve this problem:
I give the keyword to the model which creates a query, but I couldn't figure out how to put joker characters in this query.
I'm using Propel
Dennis
The filterByXXX() query functions will use LIKE when your query contains wildcards:
$books = BookQuery::create()
->filterByTitle('War%')
->find();
// example Query generated for a MySQL database
$query = 'SELECT book.* from `book` WHERE book.TITLE LIKE :p1'; // :p1 => 'War%'
Remember, the wildcards you can use in SQL are _ for exactly one and % for zero or more characters. So not ? or *.

Resources