0
我有一個NSMutable數組,用於保存通過iphone從外部傳感器捕獲的數據。我想將該數組轉儲到另一個文件中的數據庫中。我不知道它是如何完成的。將NSMutable數組傳遞到Sqlite3
Database類
- (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。
是否要更新表中的現有行或插入新行? 在您的代碼片段中,您已經有了一個如何將新行插入到表中的示例。 –
@lunatic我想插入新的表格。那些插入僅僅是一個測試。但是我在另一個類中有五個NSMutable數組,它們擁有從外部傳感器捕獲的不同值,並且它們現在都擁有雙打。所以我必須將其解壓縮爲NSNumber並將其存儲到變量中。我不知道如何將這些NSNumbers傳遞到數據庫中。 – ilaunchpad