標籤:style io os 使用 ar for 檔案 資料 div
+ (sqlite3 *)openDB
{
if (db == nil) {
//擷取Document檔案的路徑
//參數1:檔案夾名字 參數2:尋找域 參數3:是否使用絕對路徑
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//(2)資料庫檔案的路徑
NSString *dbPath = [docPath stringByAppendingPathComponent:FILE_NAME];
//ios 中管理檔案的類,負責複製檔案,刪除檔案,移動檔案(和檔案有關的操作)
NSFileManager *fm = [NSFileManager defaultManager];
//判斷document中是否有sqlite檔案
if (![fm fileExistsAtPath:dbPath])
{
//*.app中sqlite檔案的路徑;
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"DataBase" ofType:@"sqlite"];
NSError *error = nil;
//拷貝bundlePath到dbPath
BOOL result = [fm copyItemAtPath:bundlePath toPath:dbPath error:&error];
if (!result) {
NSLog(@"%@",error);//如果發生錯誤列印錯誤資訊;
}
}
//開啟資料庫
//參數1:檔案路徑; 參數2:接收資料庫的指標
sqlite3_open([dbPath UTF8String], &db);
}
return db;
}
+ (void)closeDB
{
sqlite3_close(db);
db = nil;
}
代碼實現開啟和關閉資料庫