插入或更新时,数据库在iOS中的SQLite中被锁定错误?

插入或更新时,数据库在iOS中的SQLite中被锁定错误?,ios,objective-c,sqlite,Ios,Objective C,Sqlite,我有一个CITIES表,其中存储了相同的城市名称和日期。现在,对于每个事务,如INSERT,UPDATE,都会第一次成功执行。但是,在第二次尝试时,我收到一个错误,说数据库已锁定错误。我在哪里错过了关闭和/或最终确定,我似乎不明白。造成这种混乱的原因是使用了嵌套查询,update/insert查询依赖于count查询的结果。代码流程如下所示 1] 读取数据库/重新查看数据已完成 2] 一旦数据以日期排序方式显示,就可以从表中选择新城市或现有城市 3] 这将调用“viewDataBaseForCi

我有一个CITIES表,其中存储了相同的城市名称和日期。现在,对于每个事务,如INSERT,UPDATE,都会第一次成功执行。但是,在第二次尝试时,我收到一个错误,说数据库已锁定错误。我在哪里错过了关闭和/或最终确定,我似乎不明白。造成这种混乱的原因是使用了嵌套查询,
update/insert
查询依赖于
count
查询的结果。代码流程如下所示

1] 读取数据库/重新查看数据已完成

2] 一旦数据以日期排序方式显示,就可以从表中选择新城市或现有城市

3] 这将调用“viewDataBaseForCity”方法,在该方法中完成更新或插入

4] 再次从数据库中读取数据

     //Retrieve data
-(NSMutableArray *)getCities{

const char *dbpath = [databasePath UTF8String];

@try {

    if (sqlite3_open(dbpath, &citiesDB) == SQLITE_OK)
    {

        NSString *querySQL = [NSString stringWithFormat:@"SELECT cityname FROM CITIES ORDER BY modifieddate DESC"];

        const char *query_stmt = [querySQL UTF8String];
        NSMutableArray *resultArray = [[NSMutableArray alloc]init];
        if (sqlite3_prepare_v2(citiesDB,
                               query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            while (sqlite3_step(statement) == SQLITE_ROW)
            {
                NSString *name = [[NSString alloc] initWithUTF8String:
                                  (const char *) sqlite3_column_text(statement, 0)];
                [resultArray addObject:name];

            }
            sqlite3_reset(statement);
            sqlite3_finalize(statement);
            return resultArray;
        }

    }

}
@catch (NSException *exception) {

    NSLog(@"SQL ERROR : %@",[exception reason]);

}
@finally {


    sqlite3_close(citiesDB);

  }
  return nil;

}




 //save our data

-(void)viewDataBaseForCity:(NSString *)cityName addedDate:(NSString *)dateAdded{

modifiedDate = dateAdded;
recentCity = cityName;
const char *dbpath = [databasePath UTF8String];

@try {

    if (sqlite3_open(dbpath, &citiesDB)==SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:@"SELECT COUNT(CITYNAME) FROM CITIES WHERE CITYNAME ='%@'", cityName];
        const char *query_stmt = [querySQL UTF8String];
        if (sqlite3_prepare_v2(citiesDB, query_stmt, -1, &statement, NULL)!= SQLITE_OK)
        {
            NSAssert(0, @"Failed to open database");
            NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(citiesDB), sqlite3_errcode(citiesDB));

        }else{

            if (sqlite3_step(statement)==SQLITE_ROW)
            {
                int count = sqlite3_column_int(statement, 0);
                NSLog(@"Found city count is : %d",count);
                if (count==0) {
                    //insert
                    [self insertNewCity];

                }else{
                    //update
                    [self updateCities];

                }
             sqlite3_finalize(statement);
            }else{
                NSLog(@"Not Found");
            }


        }


    }

}
@catch (NSException *exception) {
    NSLog(@"SQlite exception : %@",[exception reason]);
}
@finally {
    sqlite3_close(citiesDB);

}

}

//Insert

-(void)insertNewCity{
   NSString *insert_sql = [NSString stringWithFormat:@"INSERT INTO CITIES(cityname,modifieddate)VALUES (\'%@\',\'%@\')",recentCity,modifiedDate];

  const char *insert_stmt = [insert_sql UTF8String];
  sqlite3_prepare_v2(citiesDB, insert_stmt, -1, &statement, NULL);
  if (sqlite3_step(statement)==SQLITE_DONE)
  {
     NSLog(@"Insert Successfull");
     NSLog(@"SQLite Entry for Cities: %s and Time : %s", sqlite3_column_text(statement, 0),sqlite3_column_text(statement, 1));

  }
  else{
     NSLog(@"Insert Failed");
     NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(citiesDB), sqlite3_errcode(citiesDB));

  }
   sqlite3_finalize(statement);

 }

 //Update
 -(void)updateCities{

   NSString *querySQL = [NSString stringWithFormat:@"UPDATE CITIES set modifieddate ='%@' WHERE cityname='%@'", modifiedDate, recentCity];
   NSLog(@"Update Query : %@",querySQL);
   const char *query_stmt = [querySQL UTF8String];
   if (sqlite3_prepare_v2(citiesDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
   {
        if (sqlite3_step(statement)==SQLITE_DONE){
        NSLog(@"updated successfully");

    }else{

          NSLog(@"%s Update failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(citiesDB), sqlite3_errcode(citiesDB));
     }
     sqlite3_finalize(statement);


   }


 }

我解决了这个问题,首先进行计数,完成交易,然后决定是更新还是删除

-(void)viewDataBaseForCity:(NSString *)cityName addedDate:(NSString *)dateAdded{

 int count =0;
 modifiedDate = dateAdded;
 recentCity = cityName;
 const char *dbpath = [databasePath UTF8String];

 @try {

 if (sqlite3_open(dbpath, &citiesDB)==SQLITE_OK)
 {
     NSString *querySQL = [NSString stringWithFormat:@"SELECT COUNT(CITYNAME) FROM CITIES WHERE CITYNAME ='%@'", cityName];
     const char *query_stmt = [querySQL UTF8String];
     if (sqlite3_prepare_v2(citiesDB, query_stmt, -1, &statement, NULL)!= SQLITE_OK)
     {
         NSAssert(0, @"Failed to open database");
         NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(citiesDB), sqlite3_errcode(citiesDB));

     }else{


         if (sqlite3_step(statement)==SQLITE_ROW)
         {
             count = sqlite3_column_int(statement, 0);
             NSLog(@"Found city count is : %d",count);

         }else{
             NSLog(@"Not Found");
         }

         sqlite3_finalize(statement);

     }


 }

 if (count==0) {
     //insert
     [self insertNewCity];

 }else{
     //update
     [self updateCities];

 }




}
 @catch (NSException *exception) {
     NSLog(@"SQlite exception : %@",[exception reason]);
 }
 @finally {

     sqlite3_close(citiesDB);

 }

}

你没有关闭数据库访问。我终于要关闭了。这还不够吗?因为在
viewDataBaseForCity
中,我打开了数据库一次,所以最终关闭了它。你没有在正确的行中进行操作,傻瓜。你的“计数”查询还没有完成(太晚了)。没有必要在两次查询之间关闭数据库。它可以开几个小时。当应用程序启动时打开数据库并保持打开状态是正常的。