我有一个带有列的SQLite表,如下所述,
Table1
ID, Topics, Rating, Comments
1 topic1 3 none
2 topic1 4 none
3 topic2 2 none
4 topic2 4 none
我想使用sqlite查询为所有topic1,topic2添加评级列。 如何形成这个sqlite查询?
感谢。
答案 0 :(得分:2)
答案 1 :(得分:0)
NSString *getQuery = [NSString stringWithFormat:@"SELECT SUM(Rating) from Table1 WHERE Topics='topic1'"];
然后将其传递给下面的方法
-(NSMutableArray*) getRecords :(NSString*)getQuery isCheckOperationId:(BOOL)checkOperation
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
const char *query_stmt = [getQuery UTF8String];
sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL);
NSMutableArray *jsonArray = [NSMutableArray new];
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSMutableArray *objectColumn = [NSMutableArray new];
NSMutableArray *keysColumn = [NSMutableArray new];
for (int i=0; i < sqlite3_column_count(statement); i++)
{
[objectColumn addObject:[NSString stringWithUTF8String:(const char *) sqlite3_column_text(statement, i)]];
[keysColumn addObject:[NSString stringWithUTF8String:(const char *) sqlite3_column_name(statement, i)]];
}
NSArray *objects=[[NSArray alloc]initWithArray:objectColumn];
NSArray *keys=[[NSArray alloc]initWithArray:keysColumn];
NSDictionary *dict=[NSDictionary dictionaryWithObjects:objects forKeys:keys];
[jsonArray addObject:dict];
}
[self closeDatabase];
return jsonArray;
}
return nil;
}
希望这可以帮助你!!!