how to edit sqlite DB - ios

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"]];

Related

ios FMDB SELECT NOT IN (?)

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.

Fetching data from SQLite and want to get only the last value of column id

I am fetching data from SQLite and want to get only the last value of column id in XCode.The code is
NSString *selquery = #"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSString *valvar;
valvar = [_uid lastObject];
NSNumber *custval = [_uid valueForKey: #"#lastObject"];
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",custval,"1"];
NSLog(#"%#", imgval1);
Please tell me how can I get only the value because by using the above code I am getting array with last value of id.
I think this your case, try this it maybe help you
NSArray *temp=[NSArray arrayWithObjects:#"1",#"2",#"3", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
// NSString *tmmp=[temp0ne lastObject];
NSArray *finalStr=[uid lastObject];
NSLog(#"Dictionary is---->%#",[finalStr lastObject]);
Output:
3_1
EDIT
NSArray *temp=[NSArray arrayWithObjects:#"(1)",#"(2)",#"(3)", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
NSString *tmmp=[temp0ne lastObject];
NSString *final=[tmmp stringByReplacingOccurrencesOfString:#"(" withString:#""];
final=[final stringByReplacingOccurrencesOfString:#")" withString:#""];
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",final,"1"];
NSLog(#"%#", imgval1);
I don't know is this correct way or not try this....otherwise have look this link
I don't fully understand your code structure hehe. Try this:
NSString *selquery = #"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSNumber *custval = [_uid objectAtIndex:[_uid count]-1];
*
NSString *str = [NSString stringWithFormat#"%#",custval];
str = [str stringByReplacingOccurrencesOfString:#"("
withString:#""];
NSString *finalCustval = [NSString stringWithFormat#"%#",str];
finalCustval = [finalCustval stringByReplacingOccurrencesOfString:#")"
withString:#""];
*
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",finalCustval ,"1"];
NSLog(#"%#", imgval1);
UPDATE
try adding the ones with *.

Split NSString from first whitespace

I have a name textfield in my app, where both the firstname maybe a middle and a lastname is written. Now I want to split these components by the first whitespace, the space between the firstname and the middlename/lastname, so I can put it into my model.
For example:
Textfield Text: John D. Sowers
String 1: John
String 2: D. Sowers.
I have tried using [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] firstObject]; & [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] lastObject];
But these only work if have a name without a middlename. Since it gets the first and the last object, and the middlename is ignored.
So how would I manage to accomplish what I want?
/*fullNameString is an NSString*/
NSRange rangeOfSpace = [fullNameString rangeOfString:#" "];
NSString *first = rangeOfSpace.location == NSNotFound ? fullNameString : [fullNameString substringToIndex:rangeOfSpace.location];
NSString *last = rangeOfSpace.location == NSNotFound ? nil :[fullNameString substringFromIndex:rangeOfSpace.location + 1];
...the conditional assignment (rangeOfSpace.location == NSNotFound ? <<default value>> : <<real first/last name>>) protects against an index out of bounds error.
Well that method is giving you an array with all the words split by white space, so then you can grab the first object as the first name and the rest of the objects as middle/last/etc
NSArray *ar = [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *firstName = [ar firstObject];
NSMutableString *rest = [[NSMutableString alloc] init];
for(int i = 1; i < ar.count; i++)
{
[rest appendString:[ar objectAtIndex:i]];
[rest appendString:#" "];
}
//now first name has the first name
//rest has the rest
There might be easier way to do this, but this is one way..
Hope it helps
Daniel
I think this example below I did, solves your problem.
Remember you can assign values from the array directly, without transforming into string.
Here is an example:
NSString *textField = #"John D. Sowers";
NSArray *fullName = [textField componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]];
if (fullName.count)
{
if (fullName.count > 2)
{
NSLog(#"Array has more than 2 objects");
NSString *name = fullName[0];
NSLog(#"Name:%#",name);
NSString *middleName = fullName[1];
NSLog(#"Middle Name:%#",middleName);
NSString *lastName = fullName[2];
NSLog(#"Last Name:%#",lastName);
}
else if(fullName.count == 2)
{
NSLog(#"Array has 2 objects");
NSString *name = fullName[0];
NSLog(#"Name:%#",name);
NSString *lastName = fullName[1];
NSLog(#"Last Name:%#",lastName);
}
else
{
NSString *name = fullName[0];
}
}
I found this to be most robust:
NSString *fullNameString = #"\n Barnaby Marmaduke \n \n Aloysius ";
NSMutableArray *nameArray = [[fullNameString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
[nameArray removeObject:#""];
NSString *firstName = [nameArray firstObject];
if(nameArray.count)
{
[nameArray removeObjectAtIndex:0];
}
NSString *nameRemainder = [nameArray componentsJoinedByString:#" "];
Bob's your uncle.

uilabel text with new line only when required

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);

Error getting information from address book

I'm having an issue retrieving an array of certain data from the address book. Here's the code:
- (void) getContacts{
ABAddressBookRef ab = ABAddressBookCreate();
NSMutableArray *retVal = (__bridge NSMutableArray *)(ABAddressBookCopyArrayOfAllPeople(ab));
CFRelease(ab);
contact* temp=[[contact alloc] init];
NSMutableArray* tempArray=[[NSMutableArray alloc]init];
for(int i=0;i<[retVal count];i++)
[tempArray insertObject:
[NSString stringWithFormat:#"%#",
[temp set2:[NSString stringWithFormat:#"%#",
[[retVal objectAtIndex:i] kABPersonFirstNameProperty]]
last:[NSString stringWithFormat:#"%#",
[[retVal objectAtIndex:i] kABPersonLastNameProperty]]
number:[NSString stringWithFormat:#"%#",
[[retVal objectAtIndex:i] kABPersonPhoneProperty]]]] atIndex:i];
_objects=tempArray;
[self alert:[NSString stringWithFormat:#"%#",_objects] title:#"TEMP"];
}
The errors I'm getting are about the kABPerson properties.
Some more clarification: essentially I'm grabbing all the data from the address book into the first array and then I'm manually going through that array and attempting to retrieve the data that I need for the rest of my app.
Any ideas?
Just for more clarification here's my contact.h file:
#interface contact : NSString{
NSString* first;
NSString* last;
NSString* number;
}
#end
And here's my contact.m file:
#implementation contact
- (void) set:(NSString*)first2 last:(NSString*)last2 number:(NSString*)number2{
first=first2;
last=last2;
number=number2;
}
- (contact*) set2:(NSString*)first2 last:(NSString*)last2 number:(NSString*)number2{
first=first2;
last=last2;
number=number2;
return self;
}
#end
Here's that line that seems to be too long to post:
//Enter contact into tempArray
[tempArray insertObject:[NSString stringWithFormat:#"%#",[temp set:[NSString stringWithFormat:#"%#",(__bridge NSString *)(ABRecordCopyValue((__bridge ABMultiValueRef)[retVal objectAtIndex:i],kABPersonFirstNameProperty))] last:[NSString stringWithFormat:#"%#",(__bridge NSString *)(ABRecordCopyValue((__bridge ABMultiValueRef)[retVal objectAtIndex:i],kABPersonLastNameProperty))] number:(__bridge NSString *)ABMultiValueCopyValueAtIndex((__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)([retVal objectAtIndex:i]),kABPersonPhoneProperty)), 0)]] atIndex:i];
kABPersonFirstNameProperty is a property ID, not an Objective-C object property.
That is, you can't use [[retVal objectAtIndex:i] kABPersonFirstNameProperty] -- you will instead need to access the first and last names like this:
CFStringRef firstName = ABRecordCopyValue ([retVal objectAtIndex:i], kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue ([retVal objectAtIndex:i], kABPersonLastNameProperty);
CFStringRef phoneNum = ABRecordCopyValue ([retVal objectAtIndex:i], kABPersonPhoneProperty);
Then don't forget that ABRecordCopyValue follows the Create Rule -- you'll need to CFRelease(firstName) afterwards.

Resources