如何在Objective C命令行工具中链接SQLite3?

时间:2015-10-16 15:39:02

标签: objective-c macos sqlite command-line-interface

我正在创建一个命令行工具来创建一些数据库基准测试,但我意识到我不知道如何让SQLite3在这个环境中工作!

1 个答案:

答案 0 :(得分:1)

将此值-lsqlite3.0添加到构建设置中的其他链接器标志可解决此问题。

这是我现在可以运行的示例文件。

#import "SQLExample.h" // Nothing important in the header
#import <sqlite3.h>

@implementation SQLExample {
    sqlite3 *_database;
}

- (NSMutableArray *)sqliteExample {
    NSString *sqLiteDb = PATH_TO_DB;
    if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
        NSLog(@"Failed to open database!");
    }

    NSMutableArray *results = [[NSMutableArray alloc] init];

    NSString *query = @"SELECT A, B FROM C";

    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)
        == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            char *c_A = (char *) sqlite3_column_text(statement, 0);
            char *c_B = (char *) sqlite3_column_text(statement, 1);

            NSString *A = [[NSString alloc] initWithUTF8String:c_A];
            NSString *B = [[NSString alloc] initWithUTF8String:c_B];

            [results addObject:@[A, B]];
        }
        sqlite3_finalize(statement);
    }

    return results;
}

@end