create database code
this code correct. used code create database it's successful run , created database test application remove or delete simulator app , again run application see database not created.
please tell me actual problem face.
clsupdatenetworkviewcontroller.h
#import <uikit/uikit.h> #import "person.h" #import <sqlite3.h> @interface clsupdatenetworkviewcontroller : uiviewcontroller{ uitextfield *name; uitextfield *phone; nsstring *databasepath; sqlite3 *contactdb; int rowcount; } @property (nonatomic, strong) person *person; @property (nonatomic, strong) nsstring *firstname; @property (nonatomic, strong) nsstring *lastname; @property (nonatomic, strong) nsstring *fullname; @property (nonatomic, strong) nsstring *phonenumber; @property (nonatomic, strong) nsstring *workemail; @end clsupdatenetworkviewcontroller.m
#import "clsupdatenetworkviewcontroller.h" #import "clsmainpageappdelegate.h" #import "clsaddressbookviewcontroller.h" @interface clsupdatenetworkviewcontroller () @end @implementation clsupdatenetworkviewcontroller @synthesize person,firstname,lastname,fullname,phonenumber,workemail; @synthesize name,phone; - (void)viewdidload { [super viewdidload]; // additional setup after loading view. [self createdatabase]; } -(void) createdatabase { nsstring *docsdir; nsarray *dirpaths; // documents directory dirpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); docsdir = [dirpaths objectatindex:0]; // build path database file databasepath = [[nsstring alloc] initwithstring: [docsdir stringbyappendingpathcomponent: @"contacts.db"]]; nsfilemanager *filemgr = [nsfilemanager defaultmanager]; if ([filemgr fileexistsatpath: databasepath ] == no) { const char *dbpath = [databasepath utf8string]; if (sqlite3_open(dbpath, &contactdb) == sqlite_ok) { char *errmsg; const char *sql_stmt = "create table if not exists contacts (id integer primary key autoincrement, name text, phone text)"; if (sqlite3_exec(contactdb, sql_stmt, null, null, &errmsg) != sqlite_ok) { nslog(@"failed create table"); } sqlite3_close(contactdb); } else { nslog(@"failed open/create database"); } } } i test or debug application
code not going inside condition. if ([filemgr fileexistsatpath: databasepath ] == no) { // inside code not executed. }
call function @ didfinishlaunchingwithoptions:
-(void)createdb { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *dbpath = [documentsdirectory stringbyappendingpathcomponent:@"emp.sqlite"]; nsurl *dburl = [nsurl fileurlwithpath:dbpath]; // copy database bundle if not present // on disk nsfilemanager *fm = [nsfilemanager defaultmanager]; if (![fm fileexistsatpath:dbpath]) { nsurl *bundlepath = [[nsbundle mainbundle] urlforresource:@"emp" withextension:@"sqlite"]; nserror *error = nil; if (!bundlepath || ![fm copyitematurl:bundlepath tourl:dburl error:&error]) { nslog(@"error copying database bundle: %@", error); } } else { nslog(@"success in copy file"); } }
Comments
Post a Comment