Re: Why doesn't this work on my device?
Re: Why doesn't this work on my device?
- Subject: Re: Why doesn't this work on my device?
- From: James Cicenia <email@hidden>
- Date: Fri, 27 Feb 2009 10:44:37 -0600
Thanks for your patience. I did in fact use portions of sqlite books
and have the following code:
// Creates a writable copy of the bundled default database in the
application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory
stringByAppendingPathComponent:@"whatsfresh.sql"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to
the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"whatsfresh.sql"];
success = [fileManager copyItemAtPath:defaultDBPath
toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with
message '%@'.", [error localizedDescription]);
}
}
when I debug it, it achieves success so it does exist.
Someone wrote:
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
Don't use UTF8String to get a C string for a file system path, you
should use -fileSystemRepresentation.
I don't even know what that is?
Thanks
James
On Feb 27, 2009, at 10:35 AM, Bill Bumgarner wr, ote:
On Feb 27, 2009, at 7:56 AM, James Cicenia wrote:
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory
stringByAppendingPathComponent:@"whatsfresh.sql"];
int cntv = 0;
int cntf = 0;
ProduceItem *item=nil;
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
const char *sql = "SELECT type, subtype, name, id FROM
ProduceItem ORDER BY name";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) ==
SQLITE_OK) {
It does get to the second If statement so it did find the database.
But then evidently it is not SQLITE_OK on my device in regards to
that last if.
Have you checked to see if the file at path actually exists?
The documentation for sqlite3_open() indicates that it will quite
happily create a band new empty database if one doesn't exist at the
path.
Have a look at the SQLite Book List example (http://developer.apple.com/iphone/library/samplecode/SQLiteBooks/listing4.html
) as it demonstrates testing for the database file and then copying
it into the writable location, as necessary.
b.bum
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden