I’m working on the following code that is trying to connect to DB, but I get stuck on this point:
#import "ToDos.h"
#import "AppDelegate.h"
static sqlite3 *database = nil;
@implementation ToDos
@synthesize todoID, title, descr, isDirty, isDetailViewHydrated;
- (void) dealloc {
}
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], database) == SQLITE_OK) {
// const char *sql = "select * from todos";
// sqlite3_stmt *selectstmt;
// if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
//
// while(sqlite3_step(selectstmt) == SQLITE_ROW) {
//
// NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
// ToDos *coffeeObj = [[ToDos alloc] initWithPrimaryKey:primaryKey];
// coffeeObj.title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
//
// coffeeObj.isDirty = NO;
//
// [appDelegate.todosArray addObject:coffeeObj];
// // [coffeeObj release];
// }
// }
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
- (id) initWithPrimaryKey:(NSInteger) pk {
// [super init];
todoID = pk;
isDetailViewHydrated = NO;
return self;
}
@end
The problem seams to be in pointer….
static sqlite3 *database = nil;
and
if (sqlite3_open([dbPath UTF8String], database) == SQLITE_OK) {
but here is the error message while trying to start the app

The same code is in the tutorial an it works 🙂
As La bla bla said, it looks like you haven’t added the sqlite3 library to your project. In Xcode’s project file navigator tree on the left, click on the target (the top of the tree). When looking at the target settings, click on “Build Phases”, go to the “Link Binary With Libraries”, click on the “+” button, and add
libsqlite3.0.dylibto your project.You need
sqlite3_open([dbPath UTF8String], &database);. You’re updating yourdatabasepointer, so don’t forget that ampersand.Also, on failure, no point in calling
sqlite3_close, becausedatabasewill presumably still be NULL, possibly causing problems (crash?) if you callsqlite3_closewith a NULL database pointer because couldn’t open the database.In your commented code, you’re doing
sqlite3_prepareandsqlite3_step, but you’re not doing the finalsqlite3_finalize. It’s not fair to critique commented code, but I just want to make sure you don’t forget that when the time comes. 🙂By the way,
sqlite3_openwill create the database for you if it’s not there. If you don’t want to do that (i.e. if you only want it succeed if your previously created database is successfully found), then usesqlite3_open_v2([dbPath UTF8String], &database, SQLITE_OPEN_READWRITE, NULL);instead. Lots of first time users have a database, forget to include it in their target’s “Copy Bundle Resources” list, and are confused whensqlite3_opensuggests that they’ve successfully opened their database when in fact it might have just created a new database when it didn’t find the one you intended. If you want the opening of the database to create the database, though, ignore what I just said. But if not, considersqlite3_open_v2.Finally, once you’ve successfully opened your database, I encourage you to always check the
sqlite3_errmsgcommand if any of your subsequent commands fail. Too often some random sqlite3 command doesn’t work and the programmer is left scratching their head, but they forget to check thesqlite3_errmsg. I know that’s not the issue here, but just a final piece of counsel for a new sqlite3 programmer.