How to insert ,update,delete data in sqlite database table in iphone using ios programming?

Leave a Comment
Insert data in ios programming in Iphone. Here i am giving you simple example code snippest for it.

  #import <sqlite3.h> 
This method is used to getting correct path of sqlite file.

-(NSString*) GetDatabasePath {
 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES) ;
    NSString *documentsDirectory = [paths objectAtIndex:0] ;
   return [documentsDirectory stringByAppendingPathComponent:@"bluet.sqlite"] ;
}


 bluet.sqlite is the sqlite database file. Here I am giving you code that will create your database table using programming



Insert Querry::

    if(sqlite3_open([path UTF8String],&bluet) == SQLITE_OK)
    {
     
   
        query = [NSString stringWithFormat: @"INSERT INTO blue (bt_name, bt_description,bt_connectionid) VALUES (\"%@\", \"%@\",\"%d\")",item.name, item.description,(indexPath.row)];
     
        NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(bluet));
        if(sqlite3_prepare_v2(bluet, [query UTF8String] , -1, &statement, NULL)!= SQLITE_OK)
        {
            NSLog(@"%@",@"error");
                NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(bluet));        }
     
        if(sqlite3_step(statement)==SQLITE_DONE)
            {
             
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Completed", nil)
                                                                message:NSLocalizedString(@"Completed", nil)
                                                               delegate:nil
                                                      cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                                      otherButtonTitles:nil];
                [alert show];
            }
     
        sqlite3_finalize(statement);
    }
    sqlite3_close(bluet);

Delete Querry:
 sqlite3_stmt *statement=nil;    NSString *path = [self GetDatabasePath];    NSString *query;    if(sqlite3_open([path UTF8String],&bluet) == SQLITE_OK)    {                        query = [NSString stringWithFormat: @"delete from blue"];                NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(bluet));        if(sqlite3_prepare_v2(bluet, [query UTF8String] , -1, &statement, NULL)== SQLITE_OK)        {                        if(sqlite3_step(statement)==SQLITE_DONE)            {                                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"deleted", nil)                                                                message:NSLocalizedString(@"deleted", nil)                                                               delegate:nil                                                      cancelButtonTitle:NSLocalizedString(@"OK", nil)                                                      otherButtonTitles:nil];                [alert show];            }                        sqlite3_finalize(statement);        }        sqlite3_close(bluet); 
select Querry::  
 sqlite3_stmt *statement=nil;
    NSString *path = [self GetDatabasePath];
    NSString *query;
        if(sqlite3_open([path UTF8String],&bluet) == SQLITE_OK)
    {
        query = [NSString stringWithFormat: @"select bt_connectionid from blue"];
     
        if(sqlite3_prepare_v2(bluet, [query UTF8String], -1, &statement, NULL)== SQLITE_OK)
        {
             while (sqlite3_step(statement) == SQLITE_ROW)
            {
               NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
                NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
              int connid =  sqlite3_column_int(statement, 0);                
            }
               }
        sqlite3_finalize(statement);
    }
    sqlite3_close(bluet);
       
This is the very simple basic example code snippest in iphone and ios with sqlite database. If you want any help regarding ios programming than you can leave comment with your email id than i will send sample code example with source..Enjoy ios programming

                                                                                                       Devang Patel
                                                                                                       ios Developer

0 comments:

Post a Comment