2

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

asked Feb 1, 2012 at 7:06

1 Answer 1

1

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;
}
answered Feb 1, 2012 at 7:14
Sign up to request clarification or add additional context in comments.

8 Comments

Thnak you @Hadley for your support. but my project build & run but i m getting three warnings & even connection to database could not opend.
Mr.@Hadley what i m actually doing in above project see my next question.
probably you have not written the open database statement in your create database method. You need to open database before performing any of the DB operations (not sure about the case of select query but will be better if you open db in select operation too ).
i have written open database stmt in Implementation header. which is database.m
Can you come online ,for chat ? , I'm sure we can heal the problem just within 2 mins.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.