How to create table dynamically in sqlite database file in iphone?

Leave a Comment
Create Table in sqlite file uing iphone/ios programming. Here you have to add sqlite framwork in your application . You need to add header file:


  #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


  sqlite3_stmt *statement=nil;
    NSString *path = [self GetDatabasePath];
//    NSString *query;
    NSFileManager *fileManager = [NSFileManager defaultManager];
   BOOL success = [fileManager fileExistsAtPath:path];
    if(success)
    {
        NSLog(@"%@",@"truetreettttttrrrrrruuuuuu");
   
    }
    NSString *query;
    NSLog(@"%@",path);
   
    if(sqlite3_open([path UTF8String],&bluet) == SQLITE_OK)
    {
       
    NSString *sql_stmt =  [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS blue (bt_ID INTEGER PRIMARY KEY AUTOINCREMENT, bt_name TEXT, bt_description TEXT,bt_connectionid Integer)"];
       
        if(sqlite3_prepare_v2(bluet, [sql_stmt UTF8String] , -1, &statement, NULL)== SQLITE_OK)
        {
            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);
    }
This is the very simple code to create table in iphone.




0 comments:

Post a Comment