将NSMutablearray传递给Sqlite3

时间:2011-08-05 18:11:19

标签: objective-c database sqlite nsmutablearray

我有NSMutable数组,用于保存通过iphone从外部传感器捕获的数据。我想将该数组转储到另一个文件的数据库中。我不知道它是如何完成的。

数据库类

- (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]; 
            [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];     
}

使用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];
}

所以这里kgarray保存通过附加的外部传感器更新的所有数据。我想将kgarray持有的数据传输到ForceGaugeDatabase。

1 个答案:

答案 0 :(得分:0)

要将数组中的行插入表中,您需要遍历数组并执行查询。

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];
}