I have NSMutable array that holds data captured from external sensor through iphone. I want to dump that array into database in another file. And I have no idea how is it done.
Database class
- (id) init {
self = [super init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirecotry = [paths objectAtIndex:0];
NSString *databaseFilePath = [documentsDirecotry stringByAppendingPathComponent:@"ForceGaugeDatabase"];
BOOL databaseExists = [[NSFileManager defaultManager] fileExistsAtPath:databaseFilePath];
database = [[FMDatabase alloc] initWithPath:databaseFilePath];
if(!databaseExists){
[self createDatabase];
}
return self;
}
-(void) createDatabase{
[database open];
NSString *query = [NSString stringWithString:@"CREATE TABLE forcegauge (forcelb INTEGER, forcekg INTEGER, forcenewton INTEGER, forceoz INTEGER, analoginput INTEGER);"];
if([database executeUpdate:query]) {
for(int i = 0; i < 6 ; i++) {
query = [NSString stringWithFormat:@"insert into forcegauge (forcelb, forcekg, forcenewton, forceoz, analoginput) values (%d,20,30,40,100,9);", i];
[database executeUpdate:query];
query = [NSString stringWithFormat:@"insert into forcegauge (forcelb, forcekg, forcenewton, forceoz, analoginput) values (%d,34,45,55,90,2);", i];
[database executeUpdate:query];
query = [NSString stringWithFormat:@"insert into forcegauge (forcelb, forcekg, forcenewton, forceoz, analoginput) values (%d,35,55,65,95,3);", i];
开发者_Go百科[database executeUpdate:query];
query = [NSString stringWithFormat:@"insert into forcegauge (forcelb, forcekg, forcenewton, forceoz, analoginput) values (%d,40, 53, 67,90,4);", i];
[database executeUpdate:query];
}
} else {
NSLog(@"Error Creating Table");
}
[database close];
}
Class with NSMutableArray.
-(void) forceCalculationKg{
NSNumber *number = [controller. analogInValues objectAtIndex:0];
[controller enableDigitalInputs:YES];
double value = [number doubleValue];
double force;
force = 0.2908 *pow(2.718,(1.2089 * value));
double forcekg;
forcekg = force/2.2;
[kgarray addObject:[NSNumber numberWithDouble:forcekg]];
forceoutput.text = [NSString stringWithFormat:@" %0.1f", forcekg];
}
So here kgarray holds all the data that is being updated through an external sensor attached. And I want to transfer the data held by kgarray to ForceGaugeDatabase.
To insert rows from arrays into your table you need to iterate through arrays and execute queries.
NSUInteger totalRows = [firstArray count]; // I suppose, that each array has the same number of items
for (NSUInteger i = 0; i < totalRows; ++i) {
NSNumber *forcelb = [firstArray objectAtIndex:i];
NSNumber *forcekg = [secondArray objectAtIndex:i];
.....
NSString *query = [NSString stringWithFormat:@"insert into forcegauge (forcelb, forcekg, forcenewton, forceoz, analoginput) values (%f , %f, 53, 67,90,4);", [forcelb doubleValue], [forcekg doubleValue]];
[database executeUpdate:query];
}
精彩评论