database - Mac OS 编程中的 SQLite

标签 database macos cocoa sqlite

我是 Mac OS 编程新手,在将 SQLite 数据库集成到我的 Mac OS 项目中时遇到问题。

+ (void) getInitialDataToDisplay:(NSString *)dbPath {

    AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];


    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

        const char *sql = "select * from MyTable";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

            NSLog(@"sqlite3_step(selectstmt)=%d",sqlite3_step(selectstmt));
            NSLog(@"SQLITE_ROW= %d",SQLITE_ROW);

            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                NSInteger PrimaryKey = sqlite3_column_int(selectstmt, 0);
                DatabaseInfo *databaseObj = [[DatabaseInfo alloc] initWithPrimaryKey:PrimaryKey];
                databaseObj.item1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
                databaseObj.item2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];


                [appDelegate.mutablearray_Database addObject:databaseObj];

                [databaseObj release];
            }
        }
    }
    else
        sqlite3_close(database);

}

在上述方法中, Controller 从未进入 while 循环。这就是为什么我无法从数据库获取数据。

谁能帮帮我吗?

请向我推荐任何使用 Mac OS 编程实现 SQLite 数据库的示例代码。

最佳答案

首先,我想指出,最好使用 CoreData (苹果自己的 sqlite 实现,非常强大且高效,尽管您必须先阅读一些文档才能理解)类(class)周围)或 FMDB ,这是一个经过充分测试的 SQLite Objective-C 包装器,FMDB on github

其次,您的表中有多少行可用?由于 NSLog 也调用 sqlite3_step() 它可能只是跨过您实际拥有的唯一行..其余的实现大致类似于我在我的一些旧项目中所做的,例如:

sqlite3_stmt *statement = nil;
if( sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, NULL) == SQLITE_OK )
{
     while( sqlite3_step(statement) == SQLITE_ROW )
     {
          // get column data..
     }
}
sqlite3_finalize(statement);

关于database - Mac OS 编程中的 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13763924/

相关文章:

java - Tomcat 启动忽略用于调试的 jpda 选项

objective-c - 使用 ABAddressBook 进行内存管理

objective-c - 移除 NSButton 的蓝色阴影

sql-server - 如何回滚 UPDATE 语句?

MongoDB - 通过数组中多个对象的相同字段查询文档

c - Unix 套接字的 sockaddr_un.sun_path 区分大小写吗?

objective-c - 帮助制作自动更新程序

mysql - 根据子查询获取 COUNT 值

mysql - case 和 order by - 如何指定某些内容应显示在列表末尾?

ios - 是否有 GCD 等同于设置可以取消并在以后重新安排的计时器?