IOS packages the database files into the Mainbundle, finds no path resolution, and the possibility of deleting files in the bundle

Source: Internet
Author: User

In development sometimes we need to package the database files into our project, generally we are outside with tools to generate database files, and then pull into the project, but when we look for changes in the program file, the returned path is always nil

Workaround:

The reason we pull in other resource files (such as pictures, code files, etc.) Xcode is automatically added to Target's "Build phases" under the "Copy Bundle Resources directory, but the database files we pulled over here are not found, It's possible that Xcode doesn't recognize this type of file (when we pull it over), so all we have to do is add the db file you just added to the directory,


This will be OK, and running the code will find the path to the database file. The following code:

-(void) _testdatabasehandle{static nsstring *const database_file_name = @ "Exiu.db";    NSString *dbfilepath;                                                               Gets the path of the application Nsarray *searchpaths = Nssearchpathfordirectoriesindomains (                                                               NSDocumentDirectory, Nsuserdomainmask,    YES);    NSString *documentfolderpath = [Searchpaths objectatindex:0];        NSLog (@ "docoumentfolderpath=%@", Documentfolderpath); Add the database file name to the application path, put them together, and use the macro definition (the purpose is not easy to error) Dbfilepath = [Documentfolderpath stringbyappendingpathcomponent:d    Atabase_file_name];        NSLog (@ "Dbfilepath =%@", Dbfilepath);      /* Why add a database file to the application this process: because the following to judge, will be based on this path to find the application path in the end there is no such file, if there is, then do not copy, if not, then re-copy once,     The database file must be added aggressively, or the database cannot be manipulated, and must be added once, so why do you have to add it once? Because we implement the database modification in the program, but then add the database once, then the newly added database will overwrite the old database, then the program changes to the database can not be implemented,     1.        Create Nsfilemanager Object Nsfilemanager contains the method of file attributes Nsfilemanager *fm = [Nsfilemanager Defaultmanager]; 2.    By Nsfilemanager Object FM to determine if a file exists, there is a return yes does not exist return no BOOL isexist = [FM Fileexistsatpath:dbfilepath];        -(BOOL) Fileexistsatpath: (NSString *) path; If there is no isexist = no, copy the database from the project to documents under if (!isexist) {//Copy database//Get the path of the database in the project, because we have added several in the project According to the library file, so we're going to get the path from the project nsstring *backupdbpath = [[NSBundle Mainbundle] Pathforresource        : @ "Exiu" oftype:@ "db"]; This step is to add the database,//through the replication properties of the Nsfilemanager object, the path of the database in the project is spliced to the path of the application BOOL CP = [FM Copyitematpath:backupdbpath to        Path:dbfilepath Error:nil];        NSLog (@ "CP =%d", CP); -(BOOL) Copyitematpath: (NSString *) Srcpath Topath: (NSString *) Dstpath Error: (NSERROR *) Error NsloG (@ "Backupdbpath =%@", Backupdbpath);            if (CP) {Nserror *error;        Here the verification program runs, after copy succeeds, delete the file in mainbundle BOOL BL = [FM Removeitematpath:backupdbpath error:&error]; }} NSLog (@ "Isexist =%d", isexist);}

Following the success of the copy file, delete the source file under the main bundle is feasible

if it is under the simulator, you can run the discovery is possible! This does not mean that it is feasible on the real machine. After the real machine test, found will error!! Since the program runs on the real machine, the bundle is read-only! So do not modify any files under the program bundle at runtime!!!

As mentioned above, when the program runs, the program bundle is read-only, which is why we have to copy the data file to the document file added! This data file must be checked and manipulated.

Reference: Http://stackoverflow.com/questions/21507028/ios-sqlite-db-path-from-nsbundle-mainbundle-returns-null

Http://stackoverflow.com/questions/9002208/sqlite-db-path-in-bundle-acces

http://www.mindfiresolutions.com/Never-try-to-delete-any-resource-from-NSBundle-in-iPhoneiPadiPod-on-runtime-1523.php

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS packages the database files into the Mainbundle, finds no path resolution, and the possibility of deleting files in the bundle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.