简体   繁体   English

在Xcode中编译sqlite项目时出错

[英]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. 正如La bla bla所说 ,您似乎尚未将sqlite3库添加到项目中。 In Xcode's project file navigator tree on the left, click on the target (the top of the tree). 在左侧Xcode的项目文件导航器树中,单击目标(​​树的顶部)。 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. 查看目标设置时,单击“构建阶段”,转到“使用库链接二进制文件”,单击“ +”按钮,然后将libsqlite3.0.dylib添加到您的项目中。

  2. You need sqlite3_open([dbPath UTF8String], &database); 您需要sqlite3_open([dbPath UTF8String], &database); . You're updating your database pointer, so don't forget that ampersand. 您正在更新database指针,所以请不要忘记与号。

  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. 同样,在失败时,也没有必要调用sqlite3_close ,因为database可能仍然是NULL,如果您使用NULL数据库指针调用sqlite3_close可能会导致问题(崩溃?),因为无法打开数据库。

  4. In your commented code, you're doing sqlite3_prepare and sqlite3_step , but you're not doing the final sqlite3_finalize . 在注释的代码中,您正在执行sqlite3_preparesqlite3_step ,但是您没有执行最终的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. 顺便说一句, sqlite3_open不存在,它将为您创建数据库。 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); 如果您不想这样做(即,如果仅希望成功找到先前创建的数据库就成功),请使用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. 许多初次用户拥有数据库,却忘记将其包括在目标的“复制捆绑资源”列表中,并且在sqlite3_open建议他们已成功打开其数据库时感到困惑,而实际上它可能刚刚在创建新数据库时就打开了该数据库。找不到您想要的。 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 . 但是,如果不是,请考虑使用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. 最后,一旦成功打开数据库,我建议您始终检查sqlite3_errmsg命令, sqlite3_errmsg任何后续命令失败。 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 . 通常,某些随机的sqlite3命令不起作用,程序员被抓挠了头,但他们忘记了检查sqlite3_errmsg I know that's not the issue here, but just a final piece of counsel for a new sqlite3 programmer. 我知道这不是这里的问题,而只是新的sqlite3程序员的最后建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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