我有一个我在iOS应用程序中创建的sqlite数据库.一系列数字存储在此数据库中.我想对整个列求和,并返回要在应用程序中显示的数据.
写入数据库的所有内容都正常工作,但我仍然试图返回总结数据.任何帮助将不胜感激.
-(void) dataReturn: (NSString *) tableNamed{ NSString *myData = [NSString stringWithFormat:@"SELECT SUM(column1) AS data1 FROM myDB",tableNamed]; sqlite3_stmt *statement; if(sqlite3_prepare_v2(db,[myData UTF8String],-1,&statement,nil) ==sqlITE_OK){ while (sqlite3_step(statement) == sqlITE_ROW){ int *field2 = (int *) sqlite3_column_int(statement,1); NSString *myString =[[NSString alloc] initWithFormat:@"%@",field2]; } } }
解决方法
你好Nathan打电话给
[self dataReturn:@"Bill"];
发挥作用
-(void)dataReturn:(NSString *)tableName{ sqlite3 *database; sqlite3_stmt *statement; NSString *queryString = [NSString stringWithFormat:@"SELECT SUM(price) AS TOTAL FROM %@",tableName]; if(sqlite3_open([databasePath UTF8String],&database) == sqlITE_OK) { if(sqlite3_prepare_v2(database,[queryString UTF8String],nil) == sqlITE_OK){ while (sqlite3_step(statement) == sqlITE_ROW) { int field1 = sqlite3_column_int(statement,0); NSLog(@"The sum is %d ",field1); } } } }
将获取您想要的数据.表“Bill”的模式是“CREATE TABLE Bill(价格双倍,数量INTEGER)”.获取的结果将具有从“0”索引的列,因此我们为第一列传递0.希望你能从中得到一些暗示.干杯!!
int field1 = sqlite3_column_int(statement,0);