i want to replace my sqlite DB stores on iphone by a sqlite which is on my server for an update.
how could i do that? i can download it and get it with an NSData object but how do i store it on the iphone and then replace the old one ?
thanks
That's pretty simple. It should just be something like this:
NSData *fetchedData = ...; /* downloaded using NSURLConnection or whatever*/
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"DBName.sqlite"];
[fetchedData writeToFile:filePath atomically:YES];
Related
In my current app I am adding a data backup and restore feature.
User can create back up of database and can email it.
In restore feature user can import the database from email and can replace the current.
I am able to do create back up and restore of database file.
In restore I am just copying the file from my email to app Documents file replacing current one.
The problem is that in my code for backup of database, sqlite database is converted to NSData. I don't want to to be converted and need the exact sqlite file to be emailed.
Here is the code that I currently use to email sqlite database
-(void)backUpData
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:#"/DemoApp.sqlite"];
NSURL *newUrl = [[NSURL alloc] initWithString:
[txtPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSData *sqliteData = [[NSData alloc] initWithContentsOfURL:newUrl];
//send sqliteData to email composer delegate to attach it as attachment
[self showPicker:sqliteData];
}
How I can email sqlite db without converting it to NSData?
Use dataWithContentsOfFile when creating the NSData object giving the path rather than a url. For example:
NSString *dbPath = [[NSBundle mainBundle] pathForResource:#"Pubs" ofType:#"db"];
NSData *myData = [NSData dataWithContentsOfFile:dbPath];
[mailComposeVC addAttachmentData:myData mimeType:#"application/x-sqlite3" fileName:#"Pubs.db"];
i am using following code to log into a file...
NSData *dataToWrite = [[NSString stringWithString:#"log data"] dataUsingEncoding:NSUTF8StringEncoding];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:#"fileName.txt"];
[dataToWrite writeToFile:path atomically:YES];
But when this method gets called again...it doest show the last entry...??
Could anyone suggest?
thanks
It is better you try using NSFileHandle , because the write operation to a file on NSData simply a convenience function and can not do a full fledged file operations like appending.
Hi
I am creating a simple calculation based application and at end i need to create a text file for the calculation made in that app.Now i want that whole result into the text file, i dont if we can create a text file through our application or not but need to create that and also if we are able to create it then can we transfer to our pc/mac .
If any tutorial is available it would be of great help .
Regards
Mrugen
Try:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:#"myfile.conf"];
NSString *settings = #"1.0,0.0,0.0,0,";
NSData* settingsData;
settingsData = [settings dataUsingEncoding: NSASCIIStringEncoding];
if ([settingsData writeToFile:filePath atomically:YES])
NSLog(#"writeok");
Taken from: http://sio2interactive.forumotion.net/t347-how-to-write-a-text-file-to-iphone#1847
I am simply creating a student management system in iPhone.
There I need to store student's small images,
Which should be appear in tableView,
Ok, I know how to work with tableView...
how to work with database...
But question is
Where to store images
how can we obtain the path of stored images..
Do i have to store entire images to database...
Or
i have to store relative path of image to database...
What is suggested by You masters?
Thanks in advance for helping me.
You can create image files on the file system of the device in the Document directory. Use something like this:
// Generate a unique user filename
NSString *imageFilename = [NSString stringWithFormat:#"student_image_%#", uniqueIdentifier];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0], imageFilename];
NSData *storage = [image UIImagePNGRepresentation]; // or UIImageJPEGRepresentation
[storage writeToFile:path atomically:NO];
Best Regards,
I need to store certain information while my application is executing and again fetch it at the time the application starts. I tried storing it in XML using GData but didn't succeed.
I used the NSFileHandle it doesn't give me an error but it fails to create a .txt file for read / write purpose. Is there any other way of storing and retrieving the data on the iPhone. Below is my code for NSFileHandle.
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"myFile.txt"];
//NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:#"file://localhost/Users/shraddha/Desktop/info.txt"];
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:#"myFile.txt"];
[fh seekToEndOfFile];
NSData *data = [camName dataUsingEncoding:NSASCIIStringEncoding];
[fh writeData:data];
[fh closeFile];
For Reading
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"myFile.txt"];
//NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:#"file://localhost/Users/shraddha/Desktop/info.txt"];
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:#"myFile.txt"];
if(fh == nil)
return nil;
else
{
NSData *data = [fh readDataOfLength:8];
NSString *retStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
return retStr;
}
You're trying to write to the resource directory which you probably don't have permission to do. Try changing the first line of your code to point to the document directory:
NSArray *savePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSMutableString *savePath = [NSMutableString stringWithString:[savePaths objectAtIndex:0]];
[savePath appendString:#"/myFile.txt"];
Also, you don't need to worry about file handles. NSData is capable of writing itself to disk:
BOOL result = [data writeToFile:savePath atomically:YES];
You can use a sqlite database to store persistent data on the iPhone. Here is a blog post that should get you pointed in the right direction:
http://dblog.com.au/iphone-development-tutorials/iphone-sdk-tutorial-reading-data-from-a-sqlite-database/
Every iPhone application has at its disposal the ability to read/write name/value pairs which can be very, very useful for storing small information like user preferences. These preferences can also be edited by the user of your application (if you choose).
Another option you have which is more robust than trying to do text file storage and retrieval (I never liked this option, especially with the crappy XML parsing support on the iPhone) is sqlite. sqlite is a really light-weight relational database engine that is included with every iPhone and iPod touch.