下面我有我在哪裏獲取數據的代碼...在NSMuttable陣列添加產品不能正常工作
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);
我看到下面的輸出(用於所有IDS)
{
fullName = "test 002";
id = 7;
photoName = "22.17";
}
注
@property (nonatomic, assign) sqlite3 *database;
@property (nonatomic, retain) NSMutableArray *products;
@property (nonatomic, assign) NSUInteger currentProduct;
您是否分配了「產品」? –