我在下面的代码中获取数据...
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;