Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite Drop Table Causes Database is Locked Error

I have created a little database upgrade tool to update our sqlite database tables based on PRAGMA user_version. It has been working well until recently I needed to drop a table entirely.

obviously the drop table command is

DROP TABLE tbl;

The sql instructions are executed using:

NSString *currentUpdateStatement = [upgradeStatements objectAtIndex:statementNumber];
DLog(@"Update statement is: %@", currentUpdateStatement);
const char *sql_stmt = [currentUpdateStatement UTF8String];
char *errMsg;
//Start executing the upgrade command
if (sqlite3_exec(self.database, sql_stmt, NULL, NULL, &errMsg) == SQLITE_OK)
{
    DLog(@"Upgrade statement %i successful", statementNumber);
} else {
    NSString *errorMessage = [NSString stringWithUTF8String:errMsg];
    ELog(@"Upgrading failed at statement %i in version %i. Message: %@",
    statementNumber, newVersionNumber, errorMessage);
    [NSException raise:@"Database upgrade failed"
    format:@"Database could not be upgraded to version %i. "
    "Failed at command %i due to error '%@'",newVersionNumber, statementNumber, errorMessage];
 }

Every time I try to run a drop table command, the execution fails with 'database is locked' with a result SQLITE_LOCKED.

All other commands are completely fine. I tried to read the SQL documentation over and over with no luck. What do you guys think?

like image 706
hishamaus Avatar asked Dec 05 '25 15:12

hishamaus


1 Answers

A missing sqlite3_finalize(statement) after the antecedent transaction can cause this error.

like image 105
thpitsch Avatar answered Dec 07 '25 03:12

thpitsch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!