So my question is How can I perform or execute one or more queries in a SQLite Database one by one without Locking the database.
For example: I want to perform a delete/update on table A as well as Drop another Table B from the database when deleteBook() is called.
I have added the code. This code works sometime, other time it shows Database is Locked
func deleteBook(table: String) {
let deleteStatementStirng = "DELETE FROM book WHERE abbreviation = '(table)';"
let dropStatementString = "DROP TABLE IF EXISTS (table.replacingOccurrences(of: "-", with: "_"));"
var deleteStatement: OpaquePointer?
if sqlite3_prepare_v2(db, deleteStatementStirng, -1, &deleteStatement, nil) == SQLITE_OK {
if sqlite3_step(deleteStatement) == SQLITE_DONE {
print("Successfully deleted row.")
} else {
print("Could not delete row.")
reportError(at: "deleteBook")
}
} else {
print("DELETE statement could not be prepared")
reportError(at: "deleteBook")
}
sqlite3_finalize(deleteStatement)
var dropStatement: OpaquePointer?
if sqlite3_prepare_v2(db, dropStatementString, -1, &dropStatement, nil) ==
SQLITE_OK {
if sqlite3_step(dropStatement) == SQLITE_DONE {
print("
Successfully DROPPED TABLE.")
} else {
print("
Could not DROP TABLE.")
reportError(at: "deleteBook")
}
} else {
print("
DROP statement is not prepared")
reportError(at: "deleteBook")
}
sqlite3_finalize(dropStatement)
}
And also is calling
let db = DatabaseHelper()
in every ViewController correct or should I create Singleton class?
question from:
https://stackoverflow.com/questions/65869605/how-can-i-perform-multiple-queries-in-sqlite-database 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…