I have a CITIES
table in which I store cityname
and modifieddate
. I update the date of a city by current date/time combination and when I retrieve the city names again , I am ordering the data by DATE in DESCENDING
order.Now I have tried the queries on Firefox SQLite Manager and they work fine.However I can't find what going wrong here?.
This is how I retrieve data
//Retrieve data
-(NSMutableArray *)getCities{
const char *dbpath = [databasePath UTF8String];
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];
NSLog(@"Result Array : %@",resultArray);
}
sqlite3_finalize(statement);
sqlite3_close(citiesDB);
return resultArray;
}else{
NSLog(@"No Cities Found");
sqlite3_finalize(statement);
sqlite3_close(citiesDB);
return nil;
}
}
return nil;
}
And this is how I update the dates for city names.
//save our data / Update the date
-(void)viewDataBaseForCity:(NSString *)cityName addedDate:(NSString *)dateAdded{
@try {
const char *dbpath = [databasePath UTF8String];
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));
sqlite3_finalize(statement);
sqlite3_close(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
NSString *insert_sql = [NSString stringWithFormat:@"INSERT INTO CITIES(cityname,modifieddate)VALUES (\'%@\',\'%@\')",cityName,dateAdded];
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");
sqlite3_finalize(statement);
sqlite3_close(citiesDB);
}
else{
NSLog(@"Insert Failed"); //I also get database locked error here when tried multiple times.
NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(citiesDB), sqlite3_errcode(citiesDB));
sqlite3_finalize(statement);
sqlite3_close(citiesDB);
}
}else{
//update
NSString *querySQL = [NSString stringWithFormat:@"UPDATE CITIES set modifieddate ='%@' WHERE cityname='%@'", dateAdded, cityName];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(citiesDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(@"updated successfully");
}
sqlite3_finalize(statement);
sqlite3_close(citiesDB);
}
}else{
NSLog(@"Not Found");
}
}
}
}
@catch (NSException *exception) {
NSLog(@"SQlite exception : %@",[exception reason]);
}
@finally {
}
}
Now the date is stored as string in following format
-(NSString *)dateForCity{
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSString *dateString=[dateFormat stringFromDate:today];
return dateString;
}
Modified update query part.Which update only once and then gives database is locked error on next attempt.
//update
NSString *querySQL = [NSString stringWithFormat:@"UPDATE CITIES set modifieddate ='%@' WHERE cityname='%@'", dateAdded, cityName];
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");
sqlite3_finalize(statement);
sqlite3_close(citiesDB);
}else{
NSLog(@"%s Update failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(citiesDB), sqlite3_errcode(citiesDB));
sqlite3_finalize(statement);
sqlite3_close(citiesDB);
}
}