sqlite如何在IOS开发中应用是本文要介绍的内容,主要是来学习在IOS开发中sqlite数据库的使用方法。sqlite数据库初始化,复制到用户目录,并判断是否数据库已经存在,或者复制是否成功!
在AppDelegate.m中输入以下代码,以便复制预置数据库到指定doucment目录
- -(BOOL)initializeDb{
- NSLog(@”initializeDB”);
- //looktoseeifDBisinknownlocation(~/Documents/$DATABASE_FILE_NAME)
- //START:code.DatabaseShoppingList.findDocumentsDirectory
- NSArray*searchPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
- NSString*documentFolderPath=[searchPathsobjectAtIndex:0];
- //查看文件目录
- NSLog(@”%@”,documentFolderPath);
- dbFilePath=[documentFolderPathstringByAppendingPathComponent:@"shopping.db"];
- //END:code.DatabaseShoppingList.findDocumentsDirectory
- [dbFilePathretain];
- //START:code.DatabaseShoppingList.copyDatabaseFileToDocuments
- if(![[NSFileManagerdefaultManager]fileExistsAtPath:dbFilePath]){
- //didn’tfinddb,needtocopy
- NSString*backupDbPath=[[NSBundlemainBundle]pathForResource:@”shopping”ofType:@”db”];
- if(backupDbPath==nil){
- //couldn’tfindbackupdbtocopy,bail
- returnNO;
- }else{
- BOOLcopiedBackupDb=[[NSFileManagerdefaultManager]copyItemAtPath:backupDbPathtoPath:dbFilePatherror:nil];
- if(!copiedBackupDb){
- //copyingbackupdbFailed,bail
- returnNO;
- }
- }
- }
- returnYES;
- //END:code.DatabaseShoppingList.copyDatabaseFileToDocuments
- NSLog(@”bottomofinitializeDb”);
- }
- -(void)applicationDidFinishLaunching:(UIApplication*)application{
- //copythedatabasefromthebundleifnecessary
- if(![selfinitializeDb]){
- //TODO:alerttheuser!
- NSLog(@”couldn’tinitdb”);
- return;
- }
- //Addthetabbarcontroller’scurrentviewasasubviewofthewindow
- [windowaddSubview:tabBarController.view];
- }