Objective-c用iphone 4s插入数据

时间:2012-07-11 22:34:47

标签: iphone objective-c cocoa fmdb

我已经制作了一个应用程序,其中我使用带有fmdb包装的sqlite数据库来插入和咨询数据,数据显示在表格视图中,问题是当我在iphone 4上运行应用程序时一切正常,但是当我在iphone 4s上这样做时,它不会......有人请帮助我解决这个问题 以下是我的代码的一部分:

加载数据库:

    aPools = [[NSMutableArray alloc] init];

NSString *path = [[NSBundle mainBundle] pathForResource:@"poolsDB" ofType:@"sqlite"];

mainDB = [[FMDatabase alloc] initWithPath:path];
[mainDB open];

FMResultSet *fResult = [ mainDB executeQuery:@"SELECT * FROM poolsData"];

while ([fResult next]) {
    poolsData = [fResult stringForColumn:@"Name"];
    [aPools addObject:poolsData];  
}

[mainDB close];

保存:

-(void) saveIntoDB:(NSString *)name withVolume:(float)volume type:(NSString *)type andDesc:(NSString *)desc {

 [mainDB open];

[mainDB executeUpdate:@"INSERT INTO poolsData (Name,Volume,Type,Desc) values (?,?,?,?)",name,
 [NSNumber numberWithFloat: volume] ,type,desc, nil];

[mainDB close];

}

桌面视图是否可以以不同的方式工作?

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

用于加载:

aPools = [[NSMutableArray alloc] init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"poolsDB" ofType:@"sqlite"];

FMDatabase* mainDB = [FMDatabase databaseWithPath:path];
if(![mainDB open])
{
  return NO; //since the error in opening database
}
FMResultSet *fResult = [ mainDB executeQuery:@"SELECT * FROM poolsData"];
while ([fResult next]) {
   poolsData = [fResult stringForColumn:@"Name"];
   [aPools addObject:poolsData];  
}
[mainDB close];

SAVING的代码:

-(void) saveIntoDB:(NSString *)name withVolume:(float)volume type:(NSString *)type  
  andDesc:(NSString *)desc {

  FMDatabase* mainDB = [FMDatabase databaseWithPath:path];
  if(![mainDB open])
  {
   return; //since the error in opening database
  }
  [mainDB beginTransaction];

  [mainDB executeUpdate:@"INSERT INTO poolsData (Name,Volume,Type,Desc) VALUES  
  (?,?,?,?)",name,
  [NSNumber numberWithFloat: volume] ,type,desc];

  [mainDB commit];
  [mainDB close];
 }