Database Demo for Sqlite

AppDelegate.h

– (void) copyDatabaseIfNeeded;
– (NSString *) getDBPath;

App Delegate.m

-(void) copydb{
   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *insPath = [NSString stringWithFormat:@”UnitConverterDB.sqlite”];
    NSString *destPath = [documentsDirectory stringByAppendingPathComponent:insPath];
    NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:insPath];
    NSLog(@”\n src %@ \n dest %@”, srcPath, destPath);
    if (![[NSFileManager defaultManager] fileExistsAtPath:destPath])
    {
        NSError *error;
        NSLog(@”not exist”);
        [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:destPath error:&error];
    }else
    {
        NSLog(@”exist”);
    }
}

– (NSString *) getDBPath {
   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@”UnitConverterDB.sqlite”];
}

.h
import sqlite
sqlite3 *db;
-(NSString *) getDBPath

-(void)viewWillAppear:(BOOL)animated
{
    NSString *selectstmt=[NSString stringWithFormat:@”SELECT DISTINCT Category_Id,Cat_Name,Table_Name FROM Catergory_Master”];
   
    arrayDataFromDb=[self selectDataFromDatabase:@”UnitConverterDB.sqlite” query:selectstmt];
    NSLog(@”Return Data %@”,arrayDataFromDb);
}

.m

-(NSMutableArray *) selectDataFromDatabase:(NSString *)dbname query:(NSString *)query
{
   
    NSMutableArray *mainArray = [[NSMutableArray alloc] init];
    NSMutableDictionary *subDic = [[NSMutableDictionary alloc] init];
   
    //NSMutableArray *subArray = [[NSMutableArray alloc] init];
    NSString *dbPath = [self getDBPath];
    NSLog(@”dbPath :%@”,dbPath);
    sqlite3 *database;
    NSString * myRowData=[[NSString alloc] initWithFormat:@”%@”,@””];
    const char *sqlStatement;
    int columnCounter=0;
   
    // Open the database from the users filessytem
    if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        sqlStatement=[[NSString stringWithFormat:@”%@”,query ] UTF8String];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
        {
            columnCounter=sqlite3_column_count(compiledStatement);
            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                for(int i=0;i<columnCounter;i++)
                {
                    //NSString * test = @”Test”;
                   
                    NSString * test = ((char *)sqlite3_column_text(compiledStatement, i))  ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, i)] : @””;
                    NSString * colName = [NSString stringWithUTF8String:(char *)sqlite3_column_name(compiledStatement, i)];
                    myRowData = [myRowData stringByAppendingString:test];
                   
                    if(i != columnCounter )
                    {
                        [subDic setObject:myRowData forKey:colName];
                        myRowData = [[NSString alloc] initWithFormat:@””];
                    }
                }
                [mainArray addObject:subDic];
                subDic = nil;
                subDic = [[NSMutableDictionary alloc] init];
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
    return mainArray;
}

tableview

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{  
    static NSString *CellIdentifier = @”Cell”;
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
       
    }
   
   
    cell.textLabel.text=[[arrayDataFromDb objectAtIndex:indexPath.row] valueForKey:@”Cat_Name”];
    //cell.detailTextLabel.text=
    //[[arrayDataFromDb objectAtIndex:indexPath.row]valueForKey:@”Table_Name”];
   
    return cell;

}
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return [arrayDataFromDb count];
}
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    //[[arrayDataFromDb objectAtIndex:indexPath.row]valueForKey:@”Cat_Name” ];
    //[[arrayDataFromDb objectAtIndex:indexPath.row]valueForKey:@”Table_Name”];
    DetailViewController *detail=[[DetailViewController alloc]initWithNibName:@”DetailViewController” bundle:nil];
       detail.TableDict=[arrayDataFromDb objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
}

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 1;
}

-(NSString *) getDBPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *insPath  = [NSString stringWithFormat:@”UnitConverterDB.sqlite”];
    NSString *destPath = [documentsDirectory stringByAppendingPathComponent:insPath];
    return destPath;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s