P R J
P R J

Reputation: 428

Sqlite Database Application

I want to use same database object at Delegate file & Controller File of Custom Created databse header file which Contains object of type Sqlite3 & thee methods like OpenDB & CreateTable & InserData.

i m right now just beginner in iphone programming so, pls Help me

Upvotes: 2

Views: 360

Answers (1)

HarshIT
HarshIT

Reputation: 4905

app delegate file is like a global file , you just import the your Custom created database header file in app delegate , and create the object of Custom created database header file , assuming the file name is dbOperations.h

in app delegate

import it and create its object, then , do property , synthesize and that's all.

in any view controller you just need to import your "appDelegate.h" file and make its object in view did load like

objAppdelegate = [[UIApplication sharedApplication]delegate];

and you may access your all methods and members of dbOperations.h file by

objAppdelegate.objdbOperations .

whatever you put in appdelegate will remain shared and common to entire app , all viewControllers and all classes.

your createDatabase method should be like this, and call this method when your application did finished loading method of appDelegate file.

-(void)checkAndCreateDatabase
{   
    appDelegate = (SexarobicsAppDelegate *)[[UIApplication sharedApplication]delegate];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir =[documentPaths objectAtIndex:0];
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"database.sqlite"];
    //NSLog(@"%@", databasePath);

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if(![fileManager fileExistsAtPath:databasePath])
    {
        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    }

    //Open DB Connection
    if(sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK) 
        sqlite3_close(database);
    return; 
}

and firing sql query like this fashion, in above code @"database.sqlite" is your db file name.

-(void)getData
{
    selectStmt = nil;

     // fire query and perform those related operations

    // Release the compiled statement from memory
    sqlite3_finalize(selectStmt);
    selectStmt = nil;
}

Upvotes: 1

Related Questions