I would like to check if an id list is in the database.
Here is my code...
NSArray *array = [NSArray arrayWithObjects:#"1",#"2",#"3",nil];
NSString *string = [array componentsJoinedByString:#","];
FMResultSet * rs = [self.db executeQuery:#"SELECT id FROM FriendList WHERE id NOT IN (?)",string];
It's doesn't work. It's only work if array only has a single number.
Anyone knows how to do SELECT NOT IN using FMDB?
Thanks!
You need little modification here.
Follow below code.
NSArray *array = [NSArray arrayWithObjects:#"1",#"2",#"3",nil];
NSString *components = [array componentsJoinedByString:#", "];
NSMutableString *valueString = [NSMutableString new];
NSMutableString *fieldString = [NSMutableString new];
[fieldString appendString:#"SELECT id FROM FriendList WHERE id NOT IN ("];
for (NSString *fielValue in array) {
[fieldString appendString:#"?,"];
[valueString appendString:fielValue];
}
[fieldString replaceCharactersInRange:NSMakeRange([fieldString length] - 1, 1) withString:#")"];
NSString *normalString = [NSString stringWithString:fieldString];
fieldString = nil;
FMResultSet * rs = [self.db executeQuery:#"%#", normalString, components];
EDIT:
From the information you provide me in a comments, I did figure out that sqlite is not able to compile the string literal while you provide multiple items from array components. I modify answer still it's from your end to test that it is working or not? Leave a comment for acknowledgment.
Related
I am trying to update my NSString value with the following code.
NSString *string =[NSString stringWithFormat:#"array[0]%#",[array componentsJoinedByString:#"[0]"]];
This is the result I got.
array[0]1array[0]2array[0]3
However, I need this result, basically the number inside the brackets will be updated based on the number of objects in the NSMutablearray.
array[0]1array[1]2array[2]3
I tried using a for loop with [NSString stringWithFormat:#"[%i]", i], but it seem to not have worked.
Untested:
NSMutableString *finalString = [[NSMutableString alloc] init];
[array enumerateObjectsUsingBlock:^(NSString *str, NSUInteger idx, BOOL *stop) {
[finalString appendFormat:#"[%lu]%#", (unsigned long)idx, str];
}];
// do stuff with finalString
this is what i am using:
it works if address, city, zip.....length >0.(these field may grow in future)
self.addressInfoLbl.text = [NSString stringWithFormat:#"%#\n%#\n%#\n%#\n%#", address, city, zip, state, country];(numberofline == 0)
but if any of them length =0 then i got unnecessary new line.
i am working on manually preparing(appending \n).if there are more and more fields then doing it manuallt is really hard.
Is there any other proper way.Am i doing it right.
Thanks
Try following code. It creates array of your strings, removes empty strings and then concatenates them with componentsJoinedByString :
NSArray *strings = #[address, city, zip, state, country];
strings = [strings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"length > 0"]];
NSString *resultString = [strings componentsJoinedByString:#"\n"];
You can join an array of objects into a string with a separator:
NSArray *props = [NSArray arrayWithObjects: address, city, state, nil];
NSString *joinedString = [props componentsJoinedByString:#"\n"];
and you will get:
"6th avenue\nAtlanta\nGeorgia"
If you don't know the amount of properties, use NSMutableArray instead of NSArray and add your properties at runtime.
Try this once,
NSMutableString *joinedString=[NSMutableString string];
NSArray *arr = [NSArray arrayWithObjects: address, city, state, nil];
for(NSString *str in arr)
{
if([str length]>0) [joinedString appendFormat:#"\n%#", str];
}
NSString *resultString=[joinedString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"%#", resultString);
Lbl.numberOfLines=0;
Lbl.lineBreakMode=NSLineBreakByCharWrapping;
try this code, it not optimal but it can resolve youy issue
NSArray *arr = [NSArray arrayWithObjects: #"address", #"", #"state", nil];
NSString *addressInfo = #"";
for (NSString *str in arr) {
if (str.length > 0) {
addressInfo = [addressInfo stringByAppendingString:[NSString stringWithFormat:#"\n%#", str]];
}
}
if (addressInfo && ![#"" isEqualToString:addressInfo])
addressInfo = [addressInfo substringFromIndex:1];
NSLog(#"address Info = %#", addressInfo);
I have been struggling with this question for couple days now. Really need your help and opinion.
We have a string, that holds a text:
NSString *contentOfFile = [[NSString alloc] initWithString:#"This is string#1"];
Now I have to log symbols, that were met in this string without duplicates. Result should look like this:
whitespace symbol here
#
1
g
h
i
n
r
s
t
I know that this is solved very simple in C code using char set and iterators but I am looking for the same simple and elegant way of handling this operation in objective-c.
I was thinking of using NSCharacterSet on the string somehow but I have a lack of knowledge in objective-c so I need your help guys please. Thanks in advance to everyone who replies.
Take advantage of a trait of NSSet: Its members are distinct.
NSString *contentOfFile = #"This is string#1";
NSMutableSet *set = [NSMutableSet set];
NSUInteger length = [contentOfFile length];
for (NSUInteger index = 0; index < length; index++)
{
NSString *substring = [contentOfFile substringWithRange:NSMakeRange(index, 1)];
[set addObject:substring];
}
NSLog(#"%#", set);
However, there's one remaining problem, and that is the members of a set are also unordered. Fortunately, arrays are ordered. So if you change the last line:
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"self" ascending:YES];
NSArray *array = [set sortedArrayUsingDescriptors:#[sortDescriptor]];
NSLog(#"%#", array);
If case insensitivity is important to you, there unfortunately is no 'case-insensitive' option for NSSet. However, you could convert your source string to all lowercase, like this:
NSString *contentOfFile = [#"This is string#1" lowercaseString];
and this would give you results exactly matching your sample output.
// Create the string
NSString *contentOfFile = #"This is string#1";
// Remove all whitespaces
NSString *whitespaceRemoval = [contentOfFile stringByReplacingOccurrencesOfString:#" " withString:#""];
// Initialize an array to store the characters
NSMutableArray *components = [NSMutableArray array];
// Iterate through the characters and add them to the array
for (int i = 0; i < [whitespaceRemoval length]; i++) {
NSString *character = [NSString stringWithFormat:#"%c", [whitespaceRemoval characterAtIndex:i]];
if (![components containsObject:character]) {
[components addObject:character];
}
}
I create one application that store data from NSDictionary in SQLite. this NSDictionary get own information from many NSArray and store in SQLite DB. this is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
int number = [self GetNumberOfRecord]; //this method get number of rows in sqlite
NSArray *idd = #[#"122", #"234", #"453", #"53464", #"4565", #"1111", #"2222"];
NSArray *name = #[#"janatan", #"fred", #"john", #"cristiano", #"vein", #"emma", #"shyla"];
NSArray *age = #[#"23", #"35", #"12", #"24", #"22", #"34", #"56"];
NSArray *sex = #[#"male", #"male", #"male", #"male", #"male", #"famale", #"famale"];
NSString *query2 = [NSString stringWithFormat:#"select * from table1 where name = '%#'", [name lastObject]];
//NSLog(#"query : %#", query2);
BOOL recordExist = [self recordExistOrNot:query2];
if (!recordExist) {
for (int i = number; i < [idd count]; i++)
{
NSString *a = [idd objectAtIndex:i];
NSString *b = [name objectAtIndex:i];
NSString *c = [age objectAtIndex:i];
NSString *d = [sex objectAtIndex:i];
NSString *query = [NSString stringWithFormat:#"insert into table1 (id, name, age, sex) values ('%#', '%#', '%#', '%#')", a, b, c, d];
NSLog(#"%#", query);
[self executeQuery:query];
}
}
}
I want two things but I not found any thing about it.
First: If in above code one or two of names changed (for example NSArray *name was:
{#"janatan", #"louis", #"john", #"fredrick", #"vein", #"emma", #"shyla"}
how I can edit my table SQLite DB and replace louis & fredrick instead fred & cristiano?)
Second: If in above code one or two names removed (for example NSArray *name was:
{#"janatan", #"john", #"vein", #"emma", #"shyla"}
how I can remove Record the name of the deleted?)
thanks a lot.
for that you can use UPDATE Query.
try like this run for loop with unique identification value and update the values.
UPDATE table1 SET ContactName='newName' WHERE id=uniqu_id
Use this one.
NSString *query = [NSString stringWithFormat:#"UPDATE table1 SET name='%#' WHERE id='%#' ,[dic objectForKey:#"name"],[dic objectForKey:#"id"]];
EDIT:
NSString *query = [NSString stringWithFormat:#"UPDATE table1 SET name='%#',age='%#' WHERE id='%#' ,[dic objectForKey:#"name"],[dic objectForKey:#"age"],[dic objectForKey:#"id"]];
I want to scan NSString (case insensitive) to check whether all elements of an array contain in that String or not ?
Eg.
NSString *string1 = #"Java is pass by value : lets see how?";
NSString *string2 = #"Java is OOP Language";
NSArray *array = [[NSArray alloc] initWithObjects:#"Java",#"Pass",#"value", nil];
In this case string1 pass the test as it contain all keyword from array (i.e. Java,Pass,Value).
So, how can i achieve this functionality ?
I don't test it for speed, and it will fail on case-sensitive strings, but here's another solution (just in case)
NSArray *components = [string1 componentsSeparatedByString:#" "];
NSSet *textSet = [NSSet setWithArray:components];
NSSet *keywordsSet = [NSSet setWithArray:array];
BOOL result = [keywordsSet isSubsetOfSet:textSet];
Keep in mind that componentsSeparatedByString will tokenize very silly, like "how?" instead of "how" you need.
BOOL containsAll = YES;
for (NSString *test in array) {
if ([string1 rangeOfString:test].location == NSNotFound) {
containsAll = NO;
break;
}
}