2014-01-28 20 views
0

我是IOS開發新手,我想知道如何訪問xcode項目中的sqlite文件。在其他文件中他們這樣做:如何訪問xcode項目中的sqlite文件?

NSString *path = [mainBundle pathForResource: @"image" ofType: @"png"]; 

但是當我試着這行代碼它不能在sqlite擴展。

+0

什麼是你的sqlite文件的全稱是什麼? – KudoCC

+0

myDatabase.sqlite – NewDroidDev

+0

'NSString * path = [mainBundle pathForResource:@「myDatabase」ofType:@「sqlite」];'。它工作嗎? – KudoCC

回答

2

ios_sqlite_database

ABSQLite - 示例項目

- (void)viewDidLoad { 
     NSString *docsDir; 
     NSArray *dirPaths; 

     // 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]; 

     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)"; 

         if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) 
         { 
           status.text = @"Failed to create table"; 
         } 

         sqlite3_close(contactDB); 

       } else { 
         status.text = @"Failed to open/create database"; 
       } 
     } 

     [filemgr release]; 
     [super viewDidLoad]; 
} 

sqlite-tutorial-for-ios-making-our-app

+0

我不想通過使用stringByAppendingPathComponent手動創建我的數據庫和表。我有我自己的數據庫,它存儲在我的項目在xcode中。我想要的是訪問我的數據庫在我的xcode項目 – NewDroidDev

+0

如果你想使用coredata – codercat

相關問題