简体   繁体   中英

Error compiling sqlite project in Xcode

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 :)

  1. 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.dylib to your project.

  2. You need sqlite3_open([dbPath UTF8String], &database); . You're updating your database pointer, so don't forget that ampersand.

  3. Also, on failure, no point in calling sqlite3_close , because database will presumably still be NULL, possibly causing problems (crash?) if you call sqlite3_close with a NULL database pointer because couldn't open the database.

  4. In your commented code, you're doing sqlite3_prepare and sqlite3_step , but you're not doing the final sqlite3_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. :)

  5. By the way, sqlite3_open will create the database for you if it's not there. If you don't want to do that (ie if you only want it succeed if your previously created database is successfully found), then use sqlite3_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 when sqlite3_open suggests 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, consider sqlite3_open_v2 .

  6. Finally, once you've successfully opened your database, I encourage you to always check the sqlite3_errmsg command 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 the sqlite3_errmsg . I know that's not the issue here, but just a final piece of counsel for a new sqlite3 programmer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM