Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

ios - How can I perform multiple Queries in SQLite Database?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...