How to specify working directory for sqlite3_open - xcode

Working in xCode everything works OK if I execute by double-clicking the project down in the working directory but not from xCode itself doing a Build and Run. Then the database isn't being found correctly.
How do I modify...
sqlite3_open("Airports.sqlite", &db);
so it can find Airports.sqlite in the current working directory?

Copy your database to your applications directory and after that you can use your database :)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *dbPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"Airports.sqlite"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (!succes){
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Airports.sqlite"];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
else
NSLog(#"Succesfully created writable database");
}

Related

Cant save image locally IOS8

i have this code working on IOS7, its check if an image exist in device and if not, it download locally.
Now on IOS8 doesnt save nothing, could someone help me?
//folder where save
NSString *ImagesPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
// check if image exist
NSString* foofile = [ImagesPath stringByAppendingPathComponent:nombreImagenLocal];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
// check if image exist locally
if (!fileExists){
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:rutaCompletaLogo]];
//if not, i save it
if (data) {
// url where is saved
NSString *cachedImagePath = [ImagesPath stringByAppendingPathComponent:nombreImagenLocal];
if ([data writeToFile:cachedImagePath atomically:YES]) {
NSLog(#"Downloaded file saved to: %#", cachedImagePath);
}// end
The path to 'Documents' folder has changed from iOS8. Check the Apple tech note
Please, make sure you don't use hardcoded values. Use the methods provided by the API:
NSString *resourcePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
OR (as stated in the previous link)
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Hope this helps!

Copying file from app bundle of sandboxed application

I have a graphical application with some predefined tool configurations. At the first lunch of the application I would like to copy these default configuration files into ~/Library/Containers//Data/Library/... from the inside of the bundle. How to do that?
Try this it'll help you.
-(void)copy
{
NSFileManager *filemanager = [NSFileManager defaultManager];
NSError *error;
NSString *path = [self getPath];
BOOL sucess = [filemanager fileExistsAtPath:path];
if(!sucess){
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"/*put your assets name with extension here */"];
sucess = [filemanager copyItemAtPath:defaultPath toPath:path error:&error];
if (!sucess) {
NSAssert1(0, #"Failed '%#'.", [error localizedDescription]);
}
}
}
-(NSString *) getPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:#"/*put your assets name with extension here */"];
}

Copy data file from Xcode to iPad sandbox

I created an app in Xcode and added a text file for the app to open and display.
How can i get this text file into the iPad sandbox documents directory for testing?
There are a few things you need to do:
Create a path to your Documents directory
Make sure your text file is in your app bundle (Drag/drop into Xcode should be fine)
Copy that text file by name from the app bundle to the Documents directory
Try this:
// Documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// Destination path
NSString *fileInDocumentsPath = [documentsPath stringByAppendingPathComponent:#"name-of-your-file.txt"];
// Origin path
NSString *fileInBundlePath = [[NSBundle mainBundle] pathForResource:#"name-of-your-file" ofType:#"txt"];
// File manager for copying
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:fileInBundlePath toPath:fileInDocumentsPath error:&error];

xcode sqlite3 remove database error

I am using sqlite3 for my iOS project.
When login/logout i want to recreate the whole db.
(i can do "delete from table;" but there are ~10 tables and when i need a table more i have to be careful about writing the delete code too)
So i searched for deleting the db; but i found that it can not be done with query.
It is said that i have to remove it from filesystem;
my code is;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *dbPathDB=[appDelegate getDBPath];//dbname.sqlite
//to remove the db
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"dbname.sqlite"];
//both of them works same if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
if(filePath){
NSLog(#"it is removed now");
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
//recreating the db
if (sqlite3_open([dbPathDB UTF8String], &database) == SQLITE_OK) {
NSLog(#"db is created");
}
When i try it on device, when i first login (no removing database yet) everything is ok, when i logout and login again, i got errors about inserting the data (but it can connect to db, that part works, error is about inserting the same row again).
And i stop the device and run it again and login i got no insert errors because the db is clean..
It looks like db file is removed but db is on cache or sth like that? How can i remove and create the same db and then insert the data without errors?
This one works:
-(void)removeDB {
// Check if the SQL database has already been saved to the users phone
// if not then copy it over
BOOL success;
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
success = [fileManager fileExistsAtPath:[appDelegate getDBPath]];
if(success) {
[fileManager removeItemAtPath:[appDelegate getDBPath] error:&error];
NSLog(#"This one is the error %#",error);
return;
}
//recreating the db
if (sqlite3_open([[appDelegate getDBPath] UTF8String], &database) == SQLITE_OK) {
NSLog(#"db is created");
}
}

xcode EGODatabase update

I am trying to do a simple update to a sqlite database using EGODatabase and although the code runs the update does not occur?
NSArray *params = [NSArray arrayWithObjects:#"TEST", nil];
EGODatabase *database = [EGODatabase databaseWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"db_USBV1.sqlite3"]];
[database executeQuery:#"update users set locked = 0 where UID = ?" parameters:params ];
I saw on previous post that must copy the db to the users directory which i am doing as below;
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:#"db_USBV1.sqlite3"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
if (![fileManager fileExistsAtPath:filePath]) {
NSError *error = nil;
[fileManager copyItemAtPath:[[NSBundle mainBundle] pathForResource:#"db_USBV1" ofType:#"sqlite3"] toPath:filePath error:&error];
}
[fileManager release];
But update is not occurring.
Any help much appreciated.
Thanks,
Mike
Thanks for the pointers. I was still attempting to work with the file in the bundle and not the file that was copied.

Resources