0

I following old tutorial that use MRC, and when i pasted code i got an error: Implicit conversion to an Objective-C pointer using SQLite3 on a line:

if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {

Full code snippet is :

static FailedBankDatabase *_database;

+ (FailedBankDatabase*)database {
    if (_database == nil) {
        _database = [[FailedBankDatabase alloc] init];
    }
    return _database;
}

- (id)init {
    if ((self = [super init])) {
        NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"banklist"
                                                             ofType:@"sqlite3"];

        if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
            NSLog(@"Failed to open database!");
        }
    }
    return self;
}

Im not very keen in MRC, can you help me fix it?

2
  • Using SQLite APIs directly is generally a waste of time. There are reasons to go there, but they are rare and advanced cases. I'd suggest using CoreData or FMDB. Commented Jan 30, 2016 at 20:05
  • @bbum thank you for advice. Commented Jan 30, 2016 at 20:43

1 Answer 1

2

The _database variable is your static reference to the FailedBankDatabase singleton. But you are also trying to use it to save the SQLite database reference. For that, you need an instance variable of type sqlite3 *.

Update your code to something like the following:

@implementation FailedBankDatabase {
    sqlite3 *_db;
}

+ (FailedBankDatabase*)database {
    static FailedBankDatabase *database = nil;

    if (database == nil) {
        database = [[FailedBankDatabase alloc] init];
    }
    return database;
}

- (id)init {
    if ((self = [super init])) {
        NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"banklist"
                                                             ofType:@"sqlite3"];

        if (sqlite3_open([sqLiteDb UTF8String], &_db) != SQLITE_OK) {
            NSLog(@"Failed to open database!");
        }
    }
    return self;
}

Now use the _db variable for all database references in the various sqlite3_... function calls.

FYI - you should use a more modern approach to creating the singleton:

+ (FailedBankDatabase*)database {
    static FailedBankDatabase *database = nil;
    static dispatch_once_t predicate = 0;

    dispatch_once(&predicate, ^{
        database = [[FailedBankDatabase alloc] init];
    });

    return database;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.