muttley91
muttley91

Reputation: 12684

SQL Error: Not an Error

More SQLite issues. So my interface is as follows (this is all in .m):

@interface Search()
    @property (strong, nonatomic) NSString *databasePath; //path to sqlite database file
    @property (strong, nonatomic) NSString *databaseName;
    @property (nonatomic) sqlite3 *database;
@end

and the init follows:

- (id)init
{
    if ((self = [super init]))
    {
        self.databaseName = DB_NAME;

        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [documentPaths objectAtIndex:0];
        _databasePath = [documentsDir stringByAppendingPathComponent:self.databaseName];
        [self checkAndCreateDatabase];
        if (sqlite3_open_v2([self.databasePath UTF8String], &_database, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
        {
            [[[UIAlertView alloc]initWithTitle:@"Missing"
                                       message:@"Database file not found"
                                      delegate:nil
                             cancelButtonTitle:@"OK"
                             otherButtonTitles:nil, nil]show];
        }
        else
        {
            NSLog(@"%s: sqlite3_open_v2 error: %s", __FUNCTION__, sqlite3_errmsg(self.database));
        }
    }

The error that the Log in the init returns is: sqlite3_open_v2 error: not an error. In my searches, I've heard that SQLite doesn't return an error when it points to a non-existent database. But I'm not sure why the database wouldn't exist. The copy function I'm using (which I was given and had seemed to work before) is as follows:

-(void) checkAndCreateDatabase
{
    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    BOOL dbExists;

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    dbExists = [fileManager fileExistsAtPath:_databasePath];

    // If the database already exists then return without doing anything
    if(dbExists)
    {
        return;
    }
    // If not then proceed to copy the database from the application to the users filesystem

    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_databaseName];

    // Copy the database from the package to the users filesystem
    //[fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:nil];
    NSError *error = nil;
    if (![fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:&error])
    {
        NSLog(@"%s: copyItemAtPathError: %@", __FUNCTION__, error);
    }
}

Finally, I have verified in the iOS Simulator Documents directory that the database exists, and the query I'm trying to execute on it works. Why might I be getting this error?

Upvotes: 0

Views: 1936

Answers (2)

muttley91
muttley91

Reputation: 12684

It turns out the problem was a very simple one. The object I was trying to assign the values to (and then pull them from) was not initialized. It was a very silly oversight on my part, but I'm still new to Objective C.

Additionally, I do recommend icodebuster's suggestions to use FMDB for SQLite in iOS. It cleaned up some of my SQLite mess and made it a lot nicer to use.

Upvotes: 0

Mario
Mario

Reputation: 4520

Having never worked with SQLLite like this, I only want to mention, that in the code above, your else statement gets called, when sqlite_open_v2 == SQL_OK. So maybe in that case, there is just no error to return and everything is fine?!

Upvotes: 3

Related Questions