简体   繁体   中英

How do I store .db file in Xcode, not iPhone Simulator?

I've noticed that through my project, the .db file for SQLite that i'm using works only if I store it in the documents file of my iPhone Simulator.

How do I store it in the app itself so that I can use the db file on my iPhone instead of the simulator?

Perhaps this will help....

This is where I call it:

- (void)createOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];

dbPathString = [docPath stringByAppendingPathComponent:@"localdb.db"];


NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
    const char *dbPath = [dbPathString UTF8String];

    //creat db here
    if (sqlite3_open(dbPath, &companyDB)==SQLITE_OK) {
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS storelist (StoreID NOT NULL     PRIMARY KEY AUTOINCREMENT, StoreName TEXT NULL, StoreAddr TEXT NULL, StoreHost TEXT NULL,     StorePort TEXT NULL, StoreUser TEXT NULL, StorePass TEXT NULL, StoreDB TEXT NULL)";
        sqlite3_exec(companyDB, sql_stmt, NULL, NULL, NULL);
        sqlite3_close(companyDB);
    }
}
}

You need to have in your appDelegate.m file a method that you call in order to copy your file if it doesn't exist ex:

-(void) copyDatabaseFile
{
   BOOL success;
    NSFileManager *fileMAnager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentDirectory = [paths objectAtIndex:0];
    NSLog(@"document Directory:%@", documentDirectory);

    NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"yourFile.db/"];
    NSLog(@"Writable path:%@", writableDBPath);

    success = [fileMAnager fileExistsAtPath:writableDBPath];
    if (success) return;

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yourFile.db/"];
    NSLog(@"default database path:%@", defaultDBPath);

    success = [fileMAnager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success)
        NSAssert1(0,@"\failed to create writable database file with message'%@'.\"", [error localizedDescription]);



}

call this method in didFinishLaunchingWithOptions ...

{
   [self copyDatabaseFile];

}

then you open your file when you need it in your code...

Please notice the SQLite file at the bottom of this screenshot... 我自己的XCode项目中的sqlite db屏幕截图

This screenshot is from a current project I am working on. You can store the file in that location and then use the following code to reference it (changing the name to your own file)
NSBundle *myBundle = [NSBundle mainBundle]; NSString *absPath= [myBundle pathForResource:@"realdb" ofType:@"sqlite3"]; _databasePath = [[NSString alloc] initWithString: absPath];
(Place this code in your UIView's .m file)

Largely based on this answer to a related question:
Resources folder path in cocoa app

Use this:

- (void)createOrOpenDB
{

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:@"localdb.db"];


NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:dbPathString];
if(!success) // all the time it's YES, even if I delete database
{
    // copy database from application folder
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbPathString];
    BOOL fileExists = [fileManager fileExistsAtPath:dbPathString];
    if (fileExists) {

        const char *dbPath = [dbPathString UTF8String];

        //creat db here
        if (sqlite3_open(dbPath, &companyDB)==SQLITE_OK) {
            const char *sql_stmt = "CREATE TABLE IF NOT EXISTS storelist (StoreID NOT NULL PRIMARY KEY AUTOINCREMENT, StoreName TEXT NULL, StoreAddr TEXT NULL, StoreHost TEXT NULL, StorePort TEXT NULL, StoreUser TEXT NULL, StorePass TEXT NULL, StoreDB TEXT NULL)";
            sqlite3_exec(companyDB, sql_stmt, NULL, NULL, NULL);
            sqlite3_close(companyDB);

        [fileManager removeItemAtPath:dbPathString error:nil];
        [fileManager copyItemAtPath:databasePathFromApp toPath:dbPathString error:nil];

}}}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM