开发者

Database is locked in Sqlite

I am developing an iPhone app, when I insert data to the database I got "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while inserting data. 'database is locked''" Error. The code is :

- (NSString *) getDBPath {        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *documentsDir = [paths objectAtIndex:0];
        return [documentsDir stringByAppendingPathComponent:@"Halal.sqlite"];
}

+ (void) finalizeStatements {

            if (database) sqlite3_close(database);
            if (deleteStmt) sqlite3_finalize(deleteStmt);
            if (addStmt) sqlite3_finalize(addStmt);
            if (detailStmt) sqlite3_finalize(detailStmt);
            if (updateStmt) sqlite3_finalize(updateStmt);
}

- (void) gettingData:(NSString *)dbPath {

            NSLog(@"Data base path is %@",dbPath);
            if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
            {
                const char *sql = "select * from Product";
                sqlite3_stmt *selectstmt;
                if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
                {
                    while(sqlite3_step(selectstmt) == SQLITE_ROW)
                    {
        [membersInfoDict setValue:[NSString stringWithUTF8String:(char*)sqlite3_column_text(selectstmt, 0)] forKey:@"ProductName"];
                        [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)] forKey:@"ProductBarcode"];
                        [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)] forKey:@"ProductImage"];
                        [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)] forKey:@"ProductIngredients"];
                        [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)] forKey:@"ProductStatus"];


                        if(membersInfoDict)
                        {
                           [membersInfoArray addObject:membersInfoDict];
                           membersInfoDict = nil;
                        //  NSLog(@"Entered and return");
                            return;
                        }
                    }
                }            
            }

            else
                sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.

}


- (void) addRecord:(NSMutableDictionary *)recordDict
{
            if (sqlite3_close(database))
            {
                NSLog(@"Closed");
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
                NSString *documentsDir = [paths objectA开发者_高级运维tIndex:0];

                [self gettingData:[documentsDir stringByAppendingPathComponent:@"Halal.sqlite"]];                   
            }   
            else {
                NSLog(@"Not Closed");
            }               
            if(addStmt == nil) {
                const char *sql = "insert into Product(ProductName, ProductBarcode , ProductImage,ProductIngredients,ProductStatus ) Values(?,?,?,?,?)";
                if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
                    NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
            }

            sqlite3_bind_text(addStmt, 1, [[recordDict objectForKey:@"ProductName"] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(addStmt, 2, [[recordDict objectForKey:@"ProductBarcode"] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(addStmt, 3, [[recordDict objectForKey:@"ProductImage"] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(addStmt, 4, [[recordDict objectForKey:@"ProductIngredients"] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(addStmt, 5, [[recordDict objectForKey:@"ProductStatus"] UTF8String], -1, SQLITE_TRANSIENT);


            if(SQLITE_DONE != sqlite3_step(addStmt))
                NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
            else
            //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
                rowID = sqlite3_last_insert_rowid(database);
            NSLog(@"last inserted rowId = %d",rowID);

            //Reset the add statement.
            sqlite3_reset(addStmt);
            //sqlite3_commit_hook();
            //sqlite3_commit_hook(addStmt,[NSString stringWithFormat:@"%d",rowID],database);

}

Please give me solution for this. Thanks a lot...


Search comment //ADD THIS LINE TO YOUR CODE in following modified method of yours.

- (void) gettingData:(NSString *)dbPath {
    NSLog(@"Data base path is %@",dbPath);
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        const char *sql = "select * from Product";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
        {
            while(sqlite3_step(selectstmt) == SQLITE_ROW)
            {
                [membersInfoDict setValue:[NSString stringWithUTF8String:(char*)sqlite3_column_text(selectstmt, 0)] forKey:@"ProductName"];
                [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)] forKey:@"ProductBarcode"];
                [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)] forKey:@"ProductImage"];
                [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)] forKey:@"ProductIngredients"];
                [membersInfoDict setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)] forKey:@"ProductStatus"];

                if(membersInfoDict)
                {
                    [membersInfoArray addObject:membersInfoDict];
                    membersInfoDict = nil;
                    //  NSLog(@"Entered and return");
                    sqlite3_close(database);
                    return;
                }
            }
        }
        //ADD THIS LINE TO YOUR CODE
        sqlite3_finalize(selectstmt);
    }        
    else
        sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}


you need to finalize the compiled statements and then close the database before opening it again.

sqlite3_finalize(compiledStatement);
sqlite3_close(database);


The above answers are correct, however there is a way from which database show error of locked, if we are working or debugging on simulators.

Case 1:

Here I am explaining a scenario when I insert data into a table. And that database is accessing by any database browser and accessing from simulator.

Now when you are working on app via simulator, As you saved the data in database data saved. Now If you are using any browser for accessing data from that database and if you fire any update command in your browser and try to update any row it updates that row into a table. (Now as we try to delete anything from table it always shows an message that in browser read-only permissions are granted etc).

Now come on to simulator or If you run the app again and when you try to insert any data in that table, It always show error 'Database is locked' because the permissions are of read-only and we tempered those permissions. Now If you try to change commands as mentioned in answers you will always get sadness. It always show an error of database is locked (As I tried few times to solve this but every time I failed).

There is only a way to remove from that situation is: remove app from your simulator and reinstall it. And avoid to update/insert any row from browser.

Another Answer is: Case 2:

As you are working on sqlite browser and make any updations from it, make sure you have saved your all changes other wise locked condition happens.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜