2014-10-31 27 views
-1
{  
    NSString *docsDir; 
    NSArray *dirPaths; 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    docsDir=[NSString stringWithFormat:@"%@",dirPaths[0]]; 


    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"contacts.db"]]; 

    NSFileManager *filemgr = [[NSFileManager defaultManager]fileExistsAtPath:databasePath]; 

    const char *dbpath = [databasePath UTF8String]; 

    if (sqlite3_open (dbpath, &contactDB) == SQLITE_OK) 
    { 
     theStatus = @"asss"; 

     if ([filemgr fileExistsAtPath: databasePath ] == NO) 
     { 

      char *errMsg; 
      const char *sql_stmt = 
      "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT,PHONE TEXT)"; 

      if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg)== SQLITE_OK) 
      {   
       theStatus.text = @"database and table created"; 
      } 
      else { 
       theStatus.text = @"Failed to open/create database"; 
      }  
     } 

    } 
    [filemgr release];} 

} 
+0

如果您打算將應用程序提交給Apple App Store,則需要Xcode 5.1.1 Apple不再接受任何使用舊版Xcode版本的Apps版本。 – rckoenes 2014-10-31 12:43:37

+0

我正在嘗試這個代碼給我自己。如果這個問題的任何更好的解決方案,請幫助我 – 2014-10-31 12:48:46

+1

當errMsg失敗時,它會說什麼? – rmaddy 2014-10-31 14:40:12

回答

0

嘗試這個

-(void)databasecreation{ 

// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

docsDir = [dirPaths objectAtIndex:0]; 

// Build the path to the database file 
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"contacts.db"]]; 

NSFileManager *filemgr = [NSFileManager defaultManager]; 

//check the database is exist 
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, ADDRESS TEXT,PHONE TEXT)" UTF8String]; 


     if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK){ 
      NSLog(@"Failed to create tables"); 
     } 
     sqlite3_close(contactDB); 

    } else { 
     NSLog(@"Failed to open/create database"); 
    } 
} 
} 

更新

這一個對我的作品

appdelegate.h

#import <UIKit/UIKit.h> 
#import "sqlite3.h" 


@interface AppDelegate : UIResponder <UIApplicationDelegate>{ 
    sqlite3 *contactDB; 
    NSString *databasePath; 
} 

@property (strong, nonatomic) UIWindow *window; 
@property (nonatomic,readonly) sqlite3 *contactDB; 
@property (nonatomic,retain) NSString *databasePath; 

@end 

appdelegate.m

#import "AppDelegate.h" 

@implementation AppDelegate 
@synthesize contactDB,databasePath; 
NSString *docsDir; 
NSArray *dirPaths; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [self databasecreation]; 
    return YES; 
} 
-(void)databasecreation{ 

    // Get the documents directory 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    docsDir = [dirPaths objectAtIndex:0]; 

    // Build the path to the database file 
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"contacts.db"]]; 

    NSFileManager *filemgr = [NSFileManager defaultManager]; 

    //check the database is exist 
    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, ADDRESS TEXT,PHONE TEXT)" UTF8String]; 


      if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK){ 
       NSLog(@"Failed to create tables"); 
      } 
      sqlite3_close(contactDB); 

     } else { 
      NSLog(@"Failed to open/create database"); 
     } 
    } 
} 
+0

Noe我使用你的代碼,但同樣的答案「無法打開/創建數據庫」請幫我 – 2014-10-31 12:42:36

+0

1)爲什麼要爲'databasePath'分配一個新的字符串? 2)記錄任何失敗的'errMsg'或者調用'sqlite3_errmsg'。 – rmaddy 2014-10-31 14:41:01

相關問題