I am quite new to iOS app development.
I am trying to build a sample DB app which saves records to DB and fetches from it.
The App works well when I test with simulators. I have created a local database and also I am programatically making a copy of it when required. Also I have checked that local DB is referenced in the 'Copy Bundle Resources'.
But the error I am getting is, " * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil' ".
I think the piece of code which causes this problem is
" [FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; "
It work perfectly good for simulators but not when I test with my device.
Expecting help.
My code is,
- (id)init {
self = [super init];
//Datbase Name
DBName=#"Person.sqlite3";
//Getting DB Path
NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentsPath objectAtIndex:0];
DBPath=[documentsDir stringByAppendingPathComponent:DBName ];
//DBPath = [[NSString alloc] initWithString: [documentsDir stringByAppendingPathComponent:DBName]];
NSLog(#"DBPath is %#",DBPath);
return self;
}
-(void)checkAndCreateDB{
BOOL Success;
//NSFileManager maintains File
NSFileManager *FileManager = [NSFileManager defaultManager];
NSLog(#"line 1");
//Checks Database Path
Success = [FileManager fileExistsAtPath:DBPath];
NSLog(#"line 2");
//If file exists, it returns true
if(Success)return;
NSLog(#"line 3");
//NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];
// Get the path to the database in the application package
NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:#"Person.sqlite3" ofType:#"sqlite3"];
NSLog(#"line 4");
[FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];
//[FileManager release];
NSLog(#"line 5");
}
My Application crashes while testing in device after the line NSLOG(line 4); But works good in simulator.
Many Thanks
Prakash
The device can not write to the app bundle, you need to use the Documents directory.
Here is how to get the Documents directory path:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
If you have an initial db in the app bundle copy it to the Documents on initial startup.
change this line
NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:#"Person.sqlite3" ofType:#"sqlite3"];
to
NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:#"Person" ofType:#"sqlite3"];
also, pay attention to capitalisation, iOS devices are case sensitive, while on simulator, you'll get away with wrong capitalisation
Related
I have an iOS app which locates its database in what I believe is a fairly standard manner, viz:
+ (NSString *)dbPath
{
NSArray *a =
NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES);
NSString *dir = [a objectAtIndex:0];
return [dir stringByAppendingPathComponent:[self dbFile]];
}
Here dbFile is a function that just returns the name of the database file.
Recently users have complained that the app sometimes fails to find its database. Is there anything I am doing wrong? I have not been keeping abreast of the latest changes in iOS.
I also save db in caches in current project.Whenever user run the app,it finds db correctly.Mainly I use with FMDB.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *documentsDirectory = [documentsDir stringByAppendingPathComponent:strFileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
{
NSString *backupDbPath = [[NSBundle mainBundle] pathForResource:#"DBName" ofType:#"sqlite"];
NSLog(#"Test backuppath %#",backupDbPath);
if (backupDbPath == nil)
{
NSLog(#"Database path is nil");
}
else
{
BOOL copiedBackupDb = [[NSFileManager defaultManager] copyItemAtPath:backupDbPath toPath:documentsDirectory error:nil];
if (!copiedBackupDb) NSLog(#"Copying database failed");
}
}
Now I remember.
An app store reviewer told me that I could not store the database in the directory that I originally intended to use, because its contents were downloaded from the internet. He was wrong of course, but I was having a lot of trouble with them at the time, so I just moved the database to the cache area as requested.
That is why is sometimes gets deleted.
I am developing one iOS app in that I am using sqlite database.I have inserted records in database now I want to see that records.I searched in Library->Application Support->iPhone simulator, But in iPhone simulator folder nothing is present.So is there any another way to view sqlite database records.
Please help me,
Thank you.
This is my code for file path
-(NSString *)filePath
{
NSString *documentDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
return [documentDir stringByAppendingPathComponent:#"Test.sqlite"];
}
Now you can have your Simulator folder as "Core Simulator" not as "iPhone simulator"
Use the following Code.
// Get the Sqlite Data from the url in a NSData Object
NSData *sqlData = [[NSData alloc] initWithContentsOfURL:[
NSURL URLWithString:[sqlUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
// Store the Data locally as Sqlite File
NSString *resourceDocPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:#"Test.sqlite"];
[sqlData writeToFile:filePath atomically:YES];
NSLog(#"File Path: %#", filePath);
In Console as:
File Path:
/Users/Library/Developer/CoreSimulator/Devices/3C0492CE-2C27-48D8-AB04-7B7EFDB77228/data/Containers/Data/Application/91AF3E65-D0C2-4CC9-A7F6-35DD32AB1FD0/Documents/Test.sqlite
Now You can easily go to your database file path and open it where ever you want.
You can achieve this by using a simple code and opening the Sqlite file in to a simple Sqlite Client like chrome extension(SqliteManager), Firefox Extension and Navicat Premium.
Code:
-(NSString *)GetDocumentDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDir = [paths objectAtIndex:0];
return documentsDir;
}
-(NSString *)getDBPath
{
NSString *documentsDir = [self GetDocumentDirectory];
return [documentsDir stringByAppendingPathComponent:#"YourSqliteFileName.sqlite"];
}
Call it like this
NSString *dbPath = [self getDBPath];
NSLog(#"Database Path = %#",dbPath);
Note: Every time you will run your application on simulator, Every time a new path is created which will be different from your previous path so you have to just copy paste this path and open it in any Sqlite Client.
I'm doing a very classic routine that i've done multiple times, but in this project, its not working. When I want to work with my DB, I get " No such tables " errors. Which should be wrong. I checked the bundle database and its fine ; I checked the "result" db in the phone and its completly empty (no structure, and obviously no data).
Here is my DB creation routine. I call it every time I need the database.
+ (FMDatabase*)createAndCheckDataBase{
BOOL success;
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:#"database.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
//The database already exist in the application folder, we don't need to create it
if(success){
return [FMDatabase databaseWithPath:databasePath];
}
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"database.sqlite"];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
return [FMDatabase databaseWithPath:databasePath];
Then i just do FMDatabase *db = [DatabaseManager createAndCheckDatabase];and i supposedly get my db. But i don't.
I created the sqlite database using the SQLite Manager plugin from Firefox, created the structure there, then imported it into the bundle.
Note : success always return true.
Any help is most welcome!
As the comments mentioned, you need to check the copy.
Here's a snippet from my code which checks whether the copy succeeded and returns the error details to the consumer
//
// notice our method to prepare the db returns bool and doesn't lose the error details
// the consumer can log, present, whatever - this is a library function
//
- (BOOL)ensureDatabasePrepared: (NSError **)error
{
// <snip bunch of code>
// copy db from template to library
if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
{
NSLog(#"db not exists");
// notice how we pass in the error ref passed to this function
if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:error])
{
return NO;
}
NSLog(#"copied");
}
I have this database that i created, i want to load it using FMDB, but on the simulator i can query select the data, but not on the ipad.
i use this code to check it.
- (void)createDatabase{
//set reference to database here
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Database filename can have extension db/sqlite.
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appDBPath = [documentsDirectory stringByAppendingPathComponent:#"product.db"];
success = [fileManager fileExistsAtPath:appDBPath];
if (success) {
[self getAllData];
return;
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"product.db"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:appDBPath error:&error];
NSAssert(success, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
[self getAllData];
}
//method used to create database and check if it exists
- (void)getAllData {
// Getting the database path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:#"product.db"];
database = [FMDatabase databaseWithPath:dbPath];
[database open];
NSString *sqlSelectQuery = #"SELECT * FROM Categories";
// Query result
FMResultSet *resultsWithNameLocation = [database executeQuery:sqlSelectQuery];
while([resultsWithNameLocation next]) {
NSString *strID = [NSString stringWithFormat:#"%d",[resultsWithNameLocation intForColumn:#"id"]];
NSString *strName = [NSString stringWithFormat:#"%#",[resultsWithNameLocation stringForColumn:#"cat"]];
NSString *strLoc = [NSString stringWithFormat:#"%#",[resultsWithNameLocation stringForColumn:#"Location"]];
// loading your data into the array, dictionaries.
NSLog(#"ID = %#, Name = %#, Location = %#",strID, strName, strLoc);
}
[database close];
}
i put the product.db in the supporting files, and i copied it in the Copy Bundle Resources for the targets. Does anyone what i am doing wrong? i am kind of at a dead end here.
Thanks in advance
The problem occurs at the selecting stage, when i select it it returns:
"DB Error: 1 "no such table"
If you ever ran the app on the device before you refined the file open/copy logic, you may have accidentally created a blank database in the documents folder (because if you open a database and it's not found, it creates a blank database). Try deleting the app (which will remove any blank database that might be lingering about) and reinstalling the app and see if that fixes it.
By the way, in the future, you can avoid this problem by using openWithFlags (rather than open) with SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE), and that will ensure that the app never creates a blank database if it doesn't find one.
You can see the SQLite documentation on sqlite3_open (which FMDB open uses) and sqlite_open_v2 (which FMDB openWithFlags uses) for a discussion of these flags.
-(void)login{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:#"login" ofType:#"plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[plistDict setObject:#"si" forKey:#"stato"];
[plistDict writeToFile:path atomically: YES];
}
In iOS Simulator the plist has been correctly written, but when I try to write the .plist on my iPhone, it doesn't work. I guess it is because of the wrong .plist path.
Do the iOS devices use different path?
First you have to check if the file exits in your documents directory. If it doesn't exits there then you can copy it to the document directory. You can do it this way
-(void)login{
BOOL doesExist;
NSError *error;
NSString *filePath= [[NSBundle mainBundle] pathForResource:#"login" ofType:#"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:#"login.plist"]];
doesExist= [fileManager fileExistsAtPath:path];
if (doesExist) {
NSMutableDictionary* plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:path];
}
else
{
doesExist= [fileManager copyItemAtPath:filePath toPath:path error:&error];
NSMutableDictionary* plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}
[plistDict setObject:#"si" forKey:#"stato"];
[plistDict writeToFile:path atomically: YES];
}
You can't write to the [NSBundle mainBundle] location. In order to write files like a plist, you should save in the documents folder, this way:
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *filePathToSave = [arrayPaths objectAtIndex:0];
If the plist is part of your app, I would recommend you, in the first launch, to already copy it to the documents folder using the same filePathToSave, so you will always look at it there, both to read or to save.
This is a big mistake, as the main bundle only is readable and only composed at compile time in the App Bundle. The App Bundle lives in a separate place, whereas the data you should write to disk should be placed into the Documents, Temporary or Library folder of your sandbox.
To gain more understanding please read the official File System Programming Guide.
Everything you need to know is written there.
You can also write to subfolders and you should choose between the 3 above mentioned main directories in terms of backing up, when syncing with iTunes or iCloud. For instance contents in the tmp Folder won't be backed up.
You can not write to the mainBundle on an iOS device. You will have to save the file to a directory and modify it there.
Just to bring the answers into the modern world - you should really be using the URL based methods for getting directories:
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *URLForDocumentsDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]