I am trying a few stuff on my sqlite coding. I have successfully implemented my code for saving & deleting and is stuck now in updating. I have 1 table view controller for inserting details and another Table view controller for the showing the list. I want that when user selects the particular row in the table view controller I would again move to the EnterDetailViewController
where the details like name would be displayed on the text field. And when the user edits the data in the text field and clicks on the submit button it would come back to the table view controller and update the database as well as the table. So how to realise that.
Code for fetching and transferring the data from the table view controller-
*-(NSMutableArray *)readInformationFromDatabase
{
NSMutableArray *array = [[NSMutableArray alloc] init];
NSLog(@"fetching");
NSArray *arr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[arr objectAtIndex:0];
NSString *finalpath=[path stringByAppendingPathComponent:@"college.sqlite"];
NSLog(@"%@",finalpath);
if ([[NSFileManager defaultManager]fileExistsAtPath:finalpath])
{
sqlite3 *data;
NSString *docpath=finalpath;
NSLog(@"file to be opened-%@",docpath);
const char *ch =[docpath UTF8String];
NSLog(@"opening");
if (sqlite3_open(ch, &data)==SQLITE_OK)
{
NSLog(@"opened");
NSString *query=[NSString stringWithFormat:@"select *from student"];
NSLog(@"query-%@",query);
const char *chstmt=[query UTF8String];
sqlite3_stmt *sqlstmt;
if (sqlite3_prepare_v2(data, chstmt, -1, &sqlstmt, NULL)==SQLITE_OK)
{
while (sqlite3_step(sqlstmt)==SQLITE_ROW)
{
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
NSLog(@"fetching last stage");
const char *studname=(char *)sqlite3_column_text(sqlstmt, 0);
const char *datejoin=(char *)sqlite3_column_text(sqlstmt, 1);
NSString *studnamee=[NSString stringWithFormat:@"%s",studname];
NSString *datejoinnn=[NSString stringWithFormat:@"%s",datejoin];
NSLog(@"%@",studnamee);
NSLog(@"%@",datejoinnn);
[dict setObject:studnamee forKey:@"NAME"];
[dict setObject:datejoinnn forKey:@"DATE"];
NSLog(@"value in dict-%@",[dict valueForKey:@"NAME"]);
[array addObject:dict];
}
}
sqlite3_finalize(sqlstmt);
}
sqlite3_close(data);
}
return array;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_nametoupdateee=[[tableContent valueForKey:@"NAME"]objectAtIndex:indexPath.row];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"segueidentifier"])
{
ViewController *updatestudent=(ViewController *)[segue destinationViewController];
updatestudent.nametoupdate=self.nametoupdateee;
}
}
code for displaying it in view controller and saving the data code-
- (void)viewDidLoad {
[super viewDidLoad];
if (self.nametoupdate != NULL) {
_nametextfield.text=_nametoupdate;
}
}
- (IBAction)submit:(id)sender {
if([[NSFileManager defaultManager]fileExistsAtPath:[self getpath]])
{
[self savedata];
NSLog(@"saving");
}
else
{
NSString *str=[[NSBundle mainBundle]pathForResource:@"college" ofType:@"sqlite"];
[[NSFileManager defaultManager]copyItemAtPath:str toPath:[self getpath] error:nil];
[self savedata];
NSLog(@"creating path");
}
}
-(NSString *)getpath
{
NSArray *arr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[arr objectAtIndex:0];
NSString *finalpath=[path stringByAppendingPathComponent:@"college.sqlite"];
NSLog(@"Final Path-%@",finalpath);
return finalpath;
}
-(void)savedata
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-M-yyyy"];
NSString *dateString123= [df stringFromDate:_datepicker.date];
NSLog(@"date-%@",dateString123);
sqlite3 *database;
NSString *docpath=[self getpath];
const char *ch=[docpath UTF8String];
if (sqlite3_open(ch, &database)==SQLITE_OK)
{
NSString *query=[NSString stringWithFormat:@"INSERT into student(name,date)values(\"%@\",\"%@\")",_nametextfield.text,dateString123];
const char *chstmt=[query UTF8String];
sqlite3_stmt *sqlstmt;
if (sqlite3_prepare_v2(database, chstmt, -1, &sqlstmt, NULL)==SQLITE_OK)
{
if (SQLITE_DONE==sqlite3_step(sqlstmt))
{
_nametextfield.text=nil;
UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"saved successfully" message:@"saved" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
NSLog(@"error");
}
}
sqlite3_finalize(sqlstmt);
}
sqlite3_close(database);
}
Now my problems are-
1)When I am touching the row it is moving to the View controller but its not displaying the name retrieved on touching in the textfield. 2)How to update the database.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire