0
我有一個CITIES表,其中存儲城市名稱和日期相同。現在對於像INSERT,UPDATE這樣的每個事務,它都會在第一次成功執行。但是在第二次嘗試時,我得到一個錯誤,說數據庫被鎖定錯誤。我在哪裏錯過了close和/或finalize,我似乎不明白。混淆是由於使用嵌套查詢,其中update/insert
查詢取決於count
查詢的結果。代碼流程如下。插入或更新時,數據庫在iOS中的SQLite中出現鎖定錯誤?
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);
}
}
您沒有關閉數據庫訪問。 – 2014-10-10 18:31:11
我終於這樣做了。那還不夠嗎?因爲在'viewDataBaseForCity'中,我打開數據庫一次,最後關閉它。 – iSD 2014-10-10 18:36:38
你沒有在正確的路線上去做,很愚蠢。 – 2014-10-10 18:48:16