如何使用iphone sdk从SQLite检索数据?

如何使用iphone sdk从SQLite检索数据?,iphone,objective-c,database,sqlite,ios4,Iphone,Objective C,Database,Sqlite,Ios4,我有一个简单的sqllite数据库,有两列和多行。e、 g Code1 1000 Code2 2000 Code3 3000 Code4 4000 Code5 5000 我想将所有字段加在一起,例如code1、code2、code3、code4、code5,并将它们之间的总数返回到界面生成器中的标签中。我如何使用iphone sdk实现这一点?有没有教程?谢谢你在这方面的帮助 这里有一个很棒的教程,涵盖了UI和数据库部分: 总而言之,它将是这样的: 在databaseViewControlle

我有一个简单的sqllite数据库,有两列和多行。e、 g

Code1 1000
Code2 2000
Code3 3000
Code4 4000
Code5 5000

我想将所有字段加在一起,例如code1、code2、code3、code4、code5,并将它们之间的总数返回到界面生成器中的标签中。我如何使用iphone sdk实现这一点?有没有教程?谢谢你在这方面的帮助

这里有一个很棒的教程,涵盖了UI和数据库部分:

总而言之,它将是这样的:

在databaseViewController.h中

#import <UIKit/UIKit.h>
#import "/usr/include/sqlite3.h"

@interface databaseViewController : UIViewController {
        UILabel *total;
        NSString *databasePath;
        sqlite3 *db;
}
@property (retain, nonatomic) IBOutlet UILabel *total;
- (IBAction) getTotal;
@end
像这样的事。。。然后,只需在viewDidLoad中调用getTotal(或在按下按钮时)

#import "databaseViewController.h"

@implementation databaseViewController
@synthesize total;

-(void) getTotal
{
    sqlite3_stmt    *statement;

    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &db) == SQLITE_OK)
    {
            NSString *totalSQL = [NSString initWithUTF8String: @"SELECT SUM(field2) FROM MyTable"];
            const char *total_stmt = [totalSQL UTF8String];

            sqlite3_prepare_v2(db, total_stmt, -1, &statement, NULL);
            if (sqlite3_step(statement) == SQLITE_ROW)
            {
             NSString *totalField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
                         total.text = totalField;
            }
            sqlite3_finalize(statement);
            sqlite3_close(contactDB);
    }
}
.
.
.
- (void)viewDidUnload {
        self.total = nil;
}

- (void)dealloc {
    [total release];
        [super dealloc];
}
@end