2011-05-09 66 views
0

如何創建源碼文件時,應用程序啓動(didFinishLaunchingWithOptions),如果它已經與否exsist否則創建該文件的SQLite如何創建一個SQLite

回答

2

像這樣的測試...的SQLPATH變量是路徑您的ressource預先做好的SQL數據庫

- (void) checkAndCreateSQL 
{ 
    if (![[NSFileManager defaultManager] fileExistsAtPath:[documentPath stringByAppendingString:@"/database.sql"]]) { 
     [[NSFileManager defaultManager] createFileAtPath:[documentPath stringByAppendingString:@"/database.sql"] 
               contents:[NSData dataWithContentsOfFile:sqlPath] 
               attributes:nil]; 
    } 
} 

編輯1:

您可以使用此命令行創建你的Mac數據庫:

sqlite3 database.sql < DATABASE_CREATION.txt 
在DATABASE_CREATION.txt像這樣

CREATE TABLE IF NOT EXISTS `group` (
    `id` integer PRIMARY KEY, 
    `name` text, 
    `position` integer 
); 

然後把直接database.sql文件到您的項目資源。 (如圖像)

+0

但在這種情況下,你認爲要創建的文件已經存在,因爲你使用一個電話'[NSData的dataWithContentsOfFile: ]'。 'sqlPath'應該在哪裏? – marzapower 2011-05-09 14:36:12

+0

檢查編輯。 – TheSquad 2011-05-09 14:49:29

+0

對於數據庫的處理,除非高度特定的任務需要,否則應該使用核心數據庫來實現,我認爲,因爲使用這些數據庫,您並不受限於特定的SQL語言實現和特定的數據庫特徵。 – marzapower 2011-05-09 14:53:54

1

你可能想使用默認的核心數據庫,而不是手動創建和處理一個單一的SQLite文件。請檢查官方Apple Core Data Programming Guide。它會自動處理應用程序內部數據庫的創建和更新。

+0

我同意核心數據應該在大多數情況下使用的API,但有時你需要一個帶有主鍵和唯一鍵的數據庫,你不能只處理核心數據沒有麻煩 – TheSquad 2011-05-09 15:15:05

+0

如果核心數據無法輕鬆處理唯一或主鍵...它不會在那裏。例如,它只是使用完全獨立於SQL的方法,就像在Hibernate中發生的那樣。 – marzapower 2011-05-09 15:39:08

0
sqlite3 *reference2Database() { 
if (_database == nil) { 
    // First, test for existence. 
    NSError *error; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"my.sqlite"]; 

    if ([fileManager fileExistsAtPath:writableDBPath] == NO) { 
     // Database file doesnt exists. Copy the database at writable path (documents directory). 
     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"my.sqlite"]; 

     [fileManager removeItemAtPath:writableDBPath error:nil]; 

     BOOL databaseCopied = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
     if (!databaseCopied) { 
      // Handle the error... 
     } 
    }else { 
     // Open the database. The database was prepared outside the application. 
     if (sqlite3_open([writableDBPath UTF8String], &_database) != SQLITE_OK) { 
      // Even though the open failed, call close to properly clean up resources. 
      sqlite3_close(_database); 
      _database = nil; 
      // Additional error handling, as appropriate... 
     } 
    } 
} 
return _database; 
} 

//樣例用法。

-(void) someDatabaseFunction { 
    sqlite3 *database = reference2Database(); 
    // Do something with "database"... 
} 

//關閉數據庫。這應該在應用程序終止時調用。

void closeDatabase() { 
if (_database == nil) return; 
// Close the database. 
if (sqlite3_close(_database) != SQLITE_OK) { 
    // Handle the error... 
} 
_database = nil; 

}

注:在該文件的頂部,你應該有:靜態sqlite3的* _database =零;

0

我用Matteo Bertozzi's SQLite Wrapper與下面的代碼創建我的SQLite數據庫:

-(void)checkDatabase 
{ 
    if([[NSFileManager defaultManager] fileExistsAtPath:DBPATH] == NO) 
    { 
     sqlite = [[Sqlite alloc] init]; 

     if (![sqlite open:DBPATH]) return; 

     [sqlite executeNonQuery:@"DROP TABLE yourtable"]; 
     [sqlite executeNonQuery:@"CREATE TABLE yourtable (record1 TEXT NOT NULL, 
                  record2 TEXT NOT NULL, 
                  record3 TEXT NOT NULL, 
                  record4 TEXT NOT NULL);"]; 

     NSArray *results = [sqlite executeQuery:@"SELECT * FROM yourtable;"]; 
     for (NSDictionary *dictionary in results) { 

      for (NSString *key in [dictionary keyEnumerator]) 
       NSLog(@" - %@ %@", key, [dictionary objectForKey:key]); 
     } 

     [results release]; 
     [sqlite release]; 
    } 
}