Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

database is locked in sqlite

taniwantaniwan Posts: 2New Users
I have a problem while delete my data in sqlite.
I wanna to add and delete a data, but when I want to delete my data.
It dont work and I print out the error that said my database is locked.
Anybody can help me please?

Thanks before.

- (BOOL)addNews:(NSString *)content_caption content_date:(NSString *)content_date content_desk:(NSString *)content_desk content_id:(NSString *)content_id content_img:(NSString *)content_img content_type:(NSString *)content_type content_web_url:(NSString *)content_web_url content_related:(NSString *)content_related content_update_time:(NSString *)content_update_time content_videos:(NSString *)content_videos{
@try {

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];

dbPathString = [docPath stringByAppendingPathComponent:@"database.db"];
char *error;

arrayOfNews = [[NSMutableArray alloc] init];
if (sqlite3_open([dbPathString UTF8String], &database)==SQLITE_OK) {
NSString *inserStmt = [NSString stringWithFormat:@"INSERT INTO dbNews(CONTENT_CAPTION,CONTENT_DATE, CONTENT_DESK, CONTENT_ID, CONTENT_IMG, CONTENT_TYPE, CONTENT_WEB_URL, RELATED, UPDATE_TIME, VIDEOS) values (\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\')",content_caption, content_date, content_desk, content_id, content_img, content_type ,content_web_url, content_related, content_update_time, content_videos];

if (sqlite3_exec(database, insert_stmt, NULL, NULL, &error)==SQLITE_OK) {
News *news = [[News alloc]init];
[news setContent_caption:content_caption];
[news setContent_date:content_date];
[news setContent_desk:content_desk];
[news setContent_id:content_id];
[news setContent_img:content_img];
[news setContent_type:content_type];
[news setContent_web_url:content_web_url];
[news setContent_related:content_related];
[news setContent_update_time:content_update_time];
[news setContent_videos:content_videos];

[arrayOfNews addObject:news];
NSLog(@"Save successfull");
[arrayOfNews release];

return YES;
}else{
NSLog(@"Save failed");
return NO;
}
sqlite3_finalize(statement);
sqlite3_close(database);
}

return NO;
}
@catch (NSException *exception) {
NSLog(@"Error : %@", exception);
}@finally {
NSLog(@"Error Finally : %s", sqlite3_errmsg(database));
}
}

- (BOOL) deleteByCaption:(NSString *)registerCaption
{
@try{
sqlite3_stmt *statement;

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:@"database.db"];

if (sqlite3_open([dbPathString UTF8String], &database) == SQLITE_OK) {

NSString *sql = [NSString stringWithFormat: @"delete from dbNews where CONTENT_ID = \"%@\"", registerCaption];
const char *del_stmt = [sql UTF8String];

NSLog(@"SQL Sintax : %@", sql);
sqlite3_prepare_v2(database, del_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
return YES;
else{
NSLog(@"Error Deleting : %s", sqlite3_errmsg(database));
return NO;
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
return NO;
}@catch (NSException *er) {
NSLog(@"ERROR : %@", er);
}
}
Tagged:

Replies

  • ubunterosubunteros Posts: 40New Users @
    You don't say if your app use multi thread. If yes, you have to use @synchronize to prevent database lock error. Only 1 thread can modify you database on the same time but more can read it on the same time.
Sign In or Register to comment.