数据库查询,是数据库管理的操作中最重要的应用之一,在小节12.6.3中介绍了,数据查询的一般步骤流程。在本小结将通过示例来展示如何应用查询。
代码清单12-9 数据查询
-(void) GetList:(NSMutableArray*)KeysList { BOO L isOK = NO; sqlite3_stmt *statement; static char* sql ="Select KeyID ,Key From MBKey Order by KeyID;"; int success = sqlite3_prepare_v2(self->_database, sql, -1, &statement, NULL); if (success != SQLITE_OK) { isOK = NO; } else { //查询结果集中一条一条的遍历所有的记录,这里的数字对应的是列值。 while (sqlite3_step(statement) == SQLITE_ROW) { int kid = sqlite3_column_int(statement, 0); char* key = (char*)sqlite3_column_text(statement, 1); KeyInfo* keyInfo = [[KeyInfo alloc] init]; if(key) [keyInfo.Key setString:[NSString stringWithUTF8String:key]]; keyInfo.KeyID = kid; [KeysList addObject:keyInfo]; [keyInfo release]; } sqlite3_finalize(statement); } if (success == SQLITE_ERROR) { isOK = NO; } Else { isOK = YES; } return; }