gpt4 book ai didi

ios - 在 NSMuttable 数组中添加产品不起作用

转载 作者:行者123 更新时间:2023-11-28 19:08:40 24 4
gpt4 key购买 nike

我在下面的代码中获取数据...

database=nil;
if (sqlite3_open([sqlitePath UTF8String], &database) == SQLITE_OK){
const char *sql = [[NSString stringWithFormat:@"select * from products"] UTF8String];
sqlite3_stmt *updateStmt = nil;
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
{
//NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
if (SQLITE_DONE != sqlite3_step(updateStmt)){
//NSAssert1(0, @"Error while creating database. '%s'", sqlite3_errmsg(database));
}

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
while(sqlite3_step(updateStmt) == SQLITE_ROW) {
NSLog(@"ok=====");
NSNumber *recordID = [NSNumber numberWithInt: sqlite3_column_int(updateStmt, 0)];
NSString *fullName = [[NSString alloc] initWithUTF8String: (char *)sqlite3_column_text(updateStmt, 1)];
NSString *photoName = [[NSString alloc] initWithUTF8String: (char *)sqlite3_column_text(updateStmt, 2)];

NSLog(@"=====%@===%@====%@====", recordID, fullName, photoName);

NSDictionary *product = [[NSDictionary alloc] initWithObjectsAndKeys: recordID, @"id", fullName, @"fullName", photoName, @"photoName", nil];
[products addObject: product];
[product release];
}

NSLog(@"total data is ==== %d", [products count]);

[numberFormatter release];

sqlite3_reset(updateStmt);
sqlite3_finalize(updateStmt);

}

当我运行这段代码时,我得到如下输出。

2013-07-25 14:12:14.785 SQLTest[88568:11303] =====2===test 002====21.45====
2013-07-25 14:12:14.786 SQLTest[88568:11303] =====4===test 002====21.45====
2013-07-25 14:12:14.786 SQLTest[88568:11303] =====6===test 002====22.16====
2013-07-25 14:12:14.787 SQLTest[88568:11303] =====7===test 002====22.17====
2013-07-25 14:12:14.787 SQLTest[88568:11303] total data is ==== 0

我的问题是,当我在 products 中添加数据时,为什么我得到的 [products count] 是 0 而不是 4?

当我使用 NSLog(@"print===%@", product); 我看到下面的输出(对于所有 id)

{
fullName = "test 002";
id = 7;
photoName = "22.17";
}

注意

@property (nonatomic, assign) sqlite3 *database;
@property (nonatomic, retain) NSMutableArray *products;
@property (nonatomic, assign) NSUInteger currentProduct;

最佳答案

你有两个问题;

你还没有分配你的数组,只是声明了一个指向它的指针。将此添加到您的初始化;

products = [[NSMutableArray alloc]init];

您还通过在获取数据之前步进两次来跳过结果的第一行;

if (SQLITE_DONE != sqlite3_step(updateStmt)){     // <-- skips first row
//NSAssert1(0, @"Error while creating database. '%s'", sqlite3_errmsg(database));
}

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
while(sqlite3_step(updateStmt) == SQLITE_ROW) { // Starts at second row

关于ios - 在 NSMuttable 数组中添加产品不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17856470/

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