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

时间:2014-10-10 18:23:13

标签: ios objective-c sqlite

我有一个CITIES表,我在其中存储相同的城市名称和日期。现在,对于INSERT,UPDATE这样的每个事务,它首次成功执行。但是在第二次尝试时我收到错误说数据库被锁定错误。我错过了关闭和/或最终确定,我似乎无法理解。混淆是由于update/insert查询所依赖的嵌套查询的使用来自count查询的结果。代码流程如下。

1]读取数据库/重新获取数据。

2]数据以ORDER BY DATE方式显示。从表中选择新城市或现有城市。

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);


   }


 }

1 个答案:

答案 0 :(得分:0)

我通过先计算,完成交易,然后决定是否更新或删除来解决它。

-(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);

 }

}