Database is locked in Sqlite

后端 未结 3 836
臣服心动
臣服心动 2020-12-08 16:55

I am developing an iPhone app, when I insert data to the database I got \"Terminating app due to uncaught exception \'NSInternalInconsistencyException\', reason: \'Err

相关标签:
3条回答
  • 2020-12-08 17:33

    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 讨论(0)
  • 2020-12-08 17:55

    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.
    }
    
    0 讨论(0)
  • 2020-12-08 17:56

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

    sqlite3_finalize(compiledStatement);
    sqlite3_close(database);
    
    0 讨论(0)
提交回复
热议问题