iPhone:我的Sqlite数据库在哪里?

时间:2011-07-21 03:11:20

标签: iphone objective-c

我找不到我的SQLite数据库。我正在使用4.1模拟器。

所有教程都说它在/Users/<USERNAME>/Library/Application Support/iPhone Simulator/4.1/Applications/<APP>/Documents。那里什么都没有。如何通过代码创建新的?所以我在这里看到了.sqlite文件?

在我的代码中我这样做:

int result = sqlite3_open("/MyApp.db", &database);

if(result != SQLITE_OK)
{
sqlite3_close(database);
NSLog(@"Failed to open DB");
return;
}

else {
NSLog(@"Opened DB");
}

然后我继续创建一个表,并从中查询并且两个都成功。

然而,我找不到它将数据放在的sqlite文件。

可能在哪里?

2 个答案:

答案 0 :(得分:1)

下面有2个功能

  1. 如果在您的模拟器中安装期间它不在您的应用程序中,则首先复制您的数据库。
  2. 2.第二个获取数据库路径的函数。

    这两个函数必须调用- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions yourAppdelegate.m委托函数  这样你的数据库就会将它复制到模拟器的应用程序文件夹中(如果它不在那里)。

    不要忘记添加

    NSString *databasePath;
    sqlite3 *contactDB;
    
    yourAppdelegate.h

    中的

    - (void) copyDatabaseIfNeeded {
    
        NSFileManager *fileManager = [NSFileManager defaultManager];
    
        NSError *error;
    
        NSString *dbPath = [PropertyUploaderAppDelegate getDBPath];
    
        BOOL success = [fileManager fileExistsAtPath:dbPath];
    
        if(!success) 
    
        {
    
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] 
    
    stringByAppendingPathComponent:@"YOURDATABASENAME.sqlite"];
    
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
    
            if (!success)
    
                NSAssert1(0, @"Failed to create database file with message '%@'.", [error localizedDescription]);
    
        }
    
    }
    
    
    +(NSString *) getDBPath {
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    
        NSString *documentsDir = [paths objectAtIndex:0];
    
        return [documentsDir stringByAppendingPathComponent:@"YOURDATABASENAME.sqlite"];
    
    }
    

    经过这些过程 在/Library/Application Support/iPhone Simulator/4.1/Applications/<yourAppfolder>/Documents

    中查看

答案 1 :(得分:0)

如果您想保存在应用程序的目录中,请执行此操作,

NSString *dirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask,
                                                         YES) objectAtIndex:0];
NSString *dbFile = [dirPath stringByAppendingPathComponent:@"sqlite.db"];

如果您要打开文件/MyApp.db,那么它将位于Mac的根目录下。在Finder中打开Macintosh HD以查看它是否存在。

相关问题