gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:21:00 25 4
gpt4 key购买 nike

我有一个包含 2 列和多行的简单 sqllite 数据库。例如

Code1 1000
Code2 2000
Code3 3000
Code4 4000
Code5 5000

我想将所有字段加在一起,例如code1、code2、code3、code4、code5 并将它们之间的总和返回到我的界面生成器中的标签。我如何使用 iphone sdk 执行此操作?那里有任何教程吗?感谢您对此的任何帮助。

最佳答案

您将在此处找到涵盖 UI 和数据库部分的精彩教程:http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application

总的来说,它会是这样的:

在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

在databaseViewController.m中,

#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

类似的东西...然后您只需在 viewDidLoad 中调用 getTotal(或每当您按下按钮时)。

关于iphone - 如何使用 iphone sdk 从 SQLite 中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6087115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com