数据库被锁定在 Sqlite 中

发布于 2024-11-01 01:08:52 字数 4698 浏览 1 评论 0原文

我正在开发一个 iPhone 应用程序,当我将数据插入数据库时​​,出现“由于未捕获的异常‘NSInternalInconsistencyException’而终止应用程序,原因:‘插入数据时出错。‘数据库已锁定’”错误。 代码是:

- (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 objectAtIndex: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);

}

请给我解决方案。 多谢...

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 objectAtIndex: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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄断弥空 2024-11-08 01:08:52

在您的以下修改方法中搜索注释 //ADD THIS LINE TO YOUR CODE

- (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.
}

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.
}
度的依靠╰つ 2024-11-08 01:08:52

您需要完成编译的语句,然后关闭数据库,然后再打开它。

sqlite3_finalize(compiledStatement);
sqlite3_close(database);

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

sqlite3_finalize(compiledStatement);
sqlite3_close(database);
小忆控 2024-11-08 01:08:52

上面的答案是正确的,但是如果我们在模拟器上工作或调试,有一种方法可以使数据库显示锁定错误。

案例1:

这里我解释一个当我向表中插入数据时的场景。该数据库可以通过任何数据库浏览器访问并从模拟器访问。

现在,当您通过模拟器使用应用程序时,当您将数据保存在已保存的数据库数据中时。现在,如果您使用任何浏览器来访问该数据库中的数据,并且如果您在浏览器中触发任何更新命令并尝试更新任何行,则会将该行更新到表中。 (现在,当我们尝试从表中删除任何内容时,它总是显示一条消息,指出浏览器中已授予只读权限等)。

现在进入模拟器或者如果您再次运行该应用程序
当您尝试在该表中插入任何数据时,它总是显示错误“数据库已锁定”,因为权限是只读的,并且我们调整了这些权限。现在,如果您尝试更改答案中提到的命令,您总会感到悲伤。它总是显示数据库被锁定的错误(因为我尝试了几次来解决这个问题,但每次都失败了)。

解决这种情况的唯一方法是:从模拟器中删除应用程序并重新安装。
并避免从浏览器更新/插入任何行。

另一个答案是:
情况 2:

当您使用 sqlite 浏览器并从中进行任何更新时,请确保您已保存所有更改,否则会发生锁定情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文