I have created sqlite database.
I add this sqlite file in my application but when I run the app in the simulator and check the sqlite file's path in document directory and I open the sqlite file on document directory's path at that time the table which I have created does not appear.
In local copy I can see the table also in my project folder I can see the table but on the path of simulator I am not able to see the table
so in my console it shows me the error:
Sqlite Prepare failed: no such table
My response below assumes that your database is properly formatted and functional. You could test easily via the command line. In my own apps, all tab bar controllers apps, I place the db access code in the app delegate. The two methods in the app delegate are as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions {
[self createEditableCopyOfDatabaseIfNeeded];
[self.window setRootViewController:rootController];
[window makeKeyAndVisible];
return YES;
}
Also:
- (void)createEditableCopyOfDatabaseIfNeeded {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath =[documentsDir stringByAppendingPathComponent:#"dbName.sqlite"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"dbName.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
}
If you've tried everything and are still getting this error.. even if you've tried resetting the iOS Simulator. Try installing the app in the iOS simulator and then manually uninstalling it (select Home and long select the app icon and delete it) and then reset the simulator.
I don't know why but I tried everything else, spent hours banging my head against the wall. Then this worked.
You can put the sqlite file in your xcode project and locate it like so:
NSString *pathToSQL = [[NSBundle mainBundle]
pathForResource:#"sqlname" ofType:#"sql"];
Make sure the "Target Membership" of the sql file is set properly, so that the project "sees" it:
1) click on the sql file in the left-pane of Xcode
2) open/show the File Inspector (right pane)
3) Under "Target Membership", make sure the "check" is "checked"
That's it. Hope, this is what you're looking for. Any concern get back to me.
Related
I use ASI to download files.
But it only works if I set the path to NSDocumentDirectory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
I haven't changed any other code, just replaced NSDocumentDirectory to
NSCachesDirectory or NSDownloadsDirectory,and it does not work.
Download progress is 100% and file didn't save.
I don't know why.
This code:
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
works perfect in many projects of mine. I think the problem is in your saving code. Could you show it?
You can do it much easier and write to ANY location.
If you are using ASI, you can download DATA.(NSData)
So:
NSFileManager *fileMgr = [NSFileManager defaultManager];
[fileMgr createFileAtPath:#"whateverpathyouwant" contents:downloaddata attributes:nil];
This only works if you are jailbroken I guess.
You know how you can Reset the coredata store on an iPhone simulator when you've changed your entity structure?
Do I need to perform a similar process when I've created a new version of my core data store that is different from what I last ran on my iPhone? If so, how, please?
Thanks
Just for convenience, until you code a way to remove the persistent store through your app, you can just delete the app off the phone. (Hold your finger on the home screen until icons get wiggly, then click the x on your app.) Then with your phone connected to your Mac, choose Product > Run in XCode and it will reinstall your app on the phone, but with empty data directories.
For deployment, of course, you need to come up with a way to do it without deleting the app, if you will ever change your data model after deployment (assume you will). Data migration is the best option, but if all else fails delete the persistent store file. It would be preferable to prompt for the user's approval before doing that. If they have important data they can decline and maybe get the old version of your app back to view the data and migrate it by hand, or they can wait until you release version 2.0.1 that fixes your data migration bug.
Here is the routine I use to reset my App content. It erases the store and any other file stored.
- (void) resetContent
{
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSString * rootDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *rootURL = [NSURL fileURLWithPath:rootDir isDirectory:YES];
NSArray *content = [localFileManager contentsOfDirectoryAtURL:rootURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsSubdirectoryDescendants error:NULL];
for (NSURL *itemURL in content) {
[localFileManager removeItemAtURL:itemURL error:NULL];
}
[localFileManager release];
}
If you only want to erase the store, since you know its file name, you can refrain from enumerating the document directory content:
- (void) resetContent
{
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSString * rootDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *rootURL = [NSURL fileURLWithPath:rootDir isDirectory:YES];
NSURL *storeURL = [rootURL URLByAppendingPathComponent:#"myStore.sqlite"];
[localFileManager removeItemAtURL:storeURL error:NULL];
[localFileManager release];
}
But please note that in many cases, its better to migrate your store when you change your model, rather than to delete it.
locate your app in /Users/username/Library/Application Support/iPhone Simulator/4.3.2 (iOS Version may be different) and delete the .sqlite file
You can look at the path that is being sent to the persistentStoreCoordinator on setup, and remove that file. Usually the approach I have taken is that I set up the store to auto migrate, and if that fails I delete the store and attempt one more time to create the persistentStoreCoordinator which will use the now empty path.
Don't forget you may need to repopulate anything stored in the old database.
I have created an application included a ringtone, but how can i add it to ringtones of iphone?
Use iTunes file sharing in your app and copy the ringtone file to the app's documents directory.
Set "Application supports iTunes file sharing" to YES in your info.plist
Wherever appropriate in your app copy out the file with the code below:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyRingtone" ofType:#"m4r"];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:filePath];
[[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:#"MyRingtone.m4r"]
contents:mainBundleFile
attributes:nil];
The user can now access the ringtone via itunes and add it to their device's ringtones.
You cannot. Apple doesn't release an API to export/write ringtones to the operating system.
Is there any way of placing a file into application "sandbox" i.e into eg:
/var/mobile/Applications/30B51836-D2DD-43AA-BCB4-9D4DADFED6A2/Documents
of iphone?
My Application expects a file to be put into /Documents folder of application so that it can be uploaded to my local server.
This method should confirm to the rules of Apple so that it should not be rejected.
Why rejected? It is definitely normal to save a file to a Documents folder and Apple can not reject your application just because you save a file to the application Documents folder.
Use the following code to do so:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *pathToFile = [basePath stringByAppendingPathComponent:#"somefile.format"];
[someNSData writeToFile:pathToFile atomically: YES];
I am new to iPhone Programming
I want to read the content of text file which is in my Resourse folder. i did a lot of googling but failed to get proper way for doing this task.
Please suggest
The files in your "Resource folder" are actually the contents of your application bundle. So first you need to get the path of the file within your application bundle.
NSString* path = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"txt"];
Then loading the content into a NSString is even easier.
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
As a bonus you can have different localized versions of filename.txt and this code will fetch the file of the currently selected language correctly.
The first thing you want to do is get the path for the text file you want to read. Since it's in your Resources folder, I'm assuming you're copying it into the main bundle of the application. You can get the path for a file in your main bundle using NSBundle's pathForResource:ofType:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:#"filename" ofType:#"txt"];
Then you can read the file at that path into an NSString directly using initWithContentsOfFile:usedEncoding:error:
NSStringEncoding encoding;
NSError *error;
NSString *fileContents = [[[NSString alloc] initWithContentsOfFile:filePath
usedEncoding:&encoding
error:&error]
autorelease];