我可能做错了,因为我还是 Objective C 的新手。
我正在尝试从 sqlite3 数据库中检索数据并将其存储在 NSObject 数组中。
这是我的实现文件中的 viewDidLoad 方法。
- (void)viewDidLoad
{
[super viewDidLoad];
clients = [[NSMutableArray alloc] init];
[self openDB];
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM clients"];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
{
while (sqlite3_step(statement)==SQLITE_ROW) {
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSString *field1Str = [[NSString alloc]initWithUTF8String:field1];
char *field2 = (char *) sqlite3_column_text(statement, 1);
NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];
char *field3 = (char *) sqlite3_column_text(statement, 2);
NSString *field3Str = [[NSString alloc]initWithUTF8String:field3];
char *field4 = (char *) sqlite3_column_text(statement, 3);
NSString *field4Str = [[NSString alloc]initWithUTF8String:field4];
char *field5 = (char *) sqlite3_column_text(statement, 4);
NSString *field5Str = [[NSString alloc]initWithUTF8String:field5];
char *field6 = (char *) sqlite3_column_text(statement, 5);
NSString *field6Str = [[NSString alloc]initWithUTF8String:field6];
char *field7 = (char *) sqlite3_column_text(statement, 6);
NSString *field7Str = [[NSString alloc]initWithUTF8String:field7];
char *field8 = (char *) sqlite3_column_text(statement, 7);
NSString *field8Str = [[NSString alloc]initWithUTF8String:field8];
char *field9 = (char *) sqlite3_column_text(statement, 8);
NSString *field9Str = [[NSString alloc]initWithUTF8String:field9];
ClientObj *c1 = [[ClientObj alloc] initWithCompanyName:field1Str firstName:field2Str lastName:field3Str email:field4Str workPhone:field5Str mobilePhone:field6Str streetAddress:field7Str city:field8Str postalCode:field9Str];
}
}
self.clients = [NSArray arrayWithObjects:c1,c2,c3,c4, nil];
}
每次while语句循环通过数据库时,我都试图增加*c1。例如。c1、c2、c3、c4
然后,我尝试将从上面的数据库循环创建的每个 ClientObj 添加到客户端数组,但我不确定如何执行此操作。
非常感谢任何帮助或建议