iPhone: Where is my Sqlite database?
I can't find my SQLite DB. I'm using the 4.1 simulator.
All the tutorials say it开发者_开发技巧's at /Users/<USERNAME>/Library/Application Support/iPhone Simulator/4.1/Applications/<APP>/Documents
. There's nothing there. How do I create a new one by code? So that I see a .sqlite
file here?
In my code I am doing this:
int result = sqlite3_open("/MyApp.db", &database);
if(result != SQLITE_OK)
{
sqlite3_close(database);
NSLog(@"Failed to open DB");
return;
}
else {
NSLog(@"Opened DB");
}
I then proceed to create a table, and query from it and both of them succeed.
Yet, I can't find the sqlite file it's putting the data at.
Where could it possibly be?
Below there are 2 function
- First one to copy you database if it is not in your app during installing in ur emulator.
2.second function to get the path of database.
these 2 functions must call in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
delegate function in yourAppdelegate.m
so that your database will copy it into your application folder of the emulator if it not present there.
Don't forget to add
NSString *databasePath;
sqlite3 *contactDB;
in 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"];
}
After these process
check it in /Library/Application Support/iPhone Simulator/4.1/Applications/<yourAppfolder>/Documents
If you want to save in the app's directory then do this,
NSString *dirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
NSString *dbFile = [dirPath stringByAppendingPathComponent:@"sqlite.db"];
If you are opening a file /MyApp.db
, then it will be at the root of your Mac. Open the Macintosh HD
in Finder to see if it is there.
精彩评论