vendredi 27 novembre 2015

Primary key not set. How to delete from sqlite database

This might sound similar to many questions but my scenario is different. I have created a sqlite database "student" which has 2 columns "name & date". I am saving it in 1 view controller and displaying it in Table View Controller. Now my saving goes fine but what should I do when I need to delete the data from the database and the table view as well when user selects a particular row in the database.

Please help me with this.

Here is my coding in table view controller for fetching the data.

-(NSMutableArray *)readInformationFromDatabase
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    NSLog(@"fetching");
    NSArray *arr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[arr objectAtIndex:0];
    NSString *finalpath=[path stringByAppendingPathComponent:@"school.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;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self readInformationFromDatabase].count;


}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellreuse" forIndexPath:indexPath];


    NSLog(@"%@",[self readInformationFromDatabase]);


    cell.textLabel.text=[[[self readInformationFromDatabase] valueForKey:@"NAME"] objectAtIndex:indexPath.row ];


    cell.detailTextLabel.text=[[[self readInformationFromDatabase] valueForKey:@"DATE"] objectAtIndex:indexPath.row ];




    return cell;
}
  So what to do next to delete?

Aucun commentaire:

Enregistrer un commentaire