/*根据路径创建数据库和表*/
NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [filePath objectAtIndex:0];
NSString *dbFilePath = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"IDO_%@.sqlite",user.UserID]];
NSString *newdbFilePath = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"newIDO_%@.sqlite",user.UserID]];
/*data 为服务器返回的数据,将它写入本地,然后创建数据库,和本地文件创建的数据库进行对比合并*/
BOOL B = [data writeToFile:newdbFilePath atomically:YES];
FMDatabase* database = [ FMDatabase databaseWithPath: dbFilePath];
FMDatabase* newdatabase = [ FMDatabase databaseWithPath: newdbFilePath];
if ( ![ database open ] )
{
return;
}
if ( ![ newdatabase open ] )
{
return;
}
// 查找表 sport_day_data
FMResultSet* newstepsSet = [newdatabase executeQuery: @"select * from sport_day_data" ];
// 逐行读取数据
while ([newstepsSet next ] )
{
// 对应字段来取数据
NSString* oldsteps;
NSString* date = [newstepsSet stringForColumn: @"date" ];
FMResultSet* oldstepsSets=[database executeQuery:@"select steps from sport_day_data where date=?",date];
NSLog(@"historysteps is ---------%@",oldstepsSets);
while ([oldstepsSets next ] )
{
oldsteps = [oldstepsSets stringForColumn: @"steps" ];
NSLog(@"oldsteps is ---------%@",oldsteps);
}
NSString* newsteps = [newstepsSet stringForColumn: @"steps" ];
if(!oldsteps)
{
//今日运动把刷新来的数据重新插入数据库
VoodaDBModel *parameterModel=[[VoodaDBModel alloc]init];
parameterModel.date = date;
parameterModel.steps = newsteps;
parameterModel.total_distance = [newstepsSet stringForColumn: @"total_distance" ];
parameterModel.total_cal = [newstepsSet stringForColumn: @"total_cal" ];
parameterModel.goal_steps = [newstepsSet stringForColumn: @"goal_steps" ];
parameterModel.times = [newstepsSet stringForColumn: @"times" ];
parameterModel.hour_steps = [newstepsSet stringForColumn: @"hour_steps" ];
// VoodaDBOperation * Operation = [[VoodaDBOperation alloc] init];
// BOOL modelbool=[Operation saveCalculater:parameterModel];
NSString * proSqlStr = [NSString stringWithFormat:@"INSERT INTO sport_day_data (date,hour_steps,total_cal,steps,total_distance,times,goal_steps) VALUES ('%@','%@','%@','%@','%@','%@','%@')",parameterModel.date,parameterModel.hour_steps,parameterModel.total_cal,parameterModel.steps,parameterModel.total_distance,parameterModel.times,parameterModel.goal_steps];
bool b = [database executeUpdate:proSqlStr];
}
else
{
if([oldsteps integerValue]< [newsteps integerValue])
{
//今日运动把刷新来的数据重新插入数据库
VoodaDBModel *parameterModel=[[VoodaDBModel alloc]init];
parameterModel.date = date;
parameterModel.steps = newsteps;
parameterModel.total_distance = [newstepsSet stringForColumn: @"total_distance" ];
parameterModel.total_cal = [newstepsSet stringForColumn: @"total_cal" ];
parameterModel.goal_steps = [newstepsSet stringForColumn: @"goal_steps" ];
parameterModel.times = [newstepsSet stringForColumn: @"times" ];
parameterModel.hour_steps = [newstepsSet stringForColumn: @"hour_steps" ];
// VoodaDBOperation * Operation = [[VoodaDBOperation alloc] init];
// BOOL modelbool=[Operation saveCalculater:parameterModel];
NSString *proSqlStr = [NSString stringWithFormat:@"UPDATE sport_day_data SET date='%@',hour_steps='%@',total_cal='%@',steps='%@',total_distance='%@',times='%@',goal_steps='%@' where date='%@'",parameterModel.date,parameterModel.hour_steps,parameterModel.total_cal,parameterModel.steps,parameterModel.total_distance,parameterModel.times,parameterModel.goal_steps,parameterModel.date];
bool b = [database executeUpdate:proSqlStr];
}
}
NSLog(@"newsteps is ---------%@",newsteps);
}