【问题标题】:Error using SQLite Swift4: multi-threaded access to database connection使用 SQLite Swift4 时出错:多线程访问数据库连接
【发布时间】:2018-06-21 00:22:43
【问题描述】:

我需要将大部分数据保存在我的数据库中,并得到以下错误:

[logging] BUG IN CLIENT OF sqlite3.dylib: illegal multi-threaded access to database connection

我的插入函数:

static func insert( product : [String:Any] ) -> Bool {
        print("\nInsert: \(product["display_name"]!)\n")

        // PREPARA A QUERY
        var auxStr: String = "INSERT INTO product ("
        var auxValues: String = " VALUES ("

        for s in fieldsProducts {
            if fieldsProducts.last == s {
                auxStr += s + ")"
                auxValues += "?);"
            } else {
                auxStr += s + ","
                auxValues += "?,"
            }
        }

        //print("Query: \(auxStr) \n Values: \(auxValues)")
        let insertQuery = auxStr + auxValues
        print("Query: \(insertQuery)")
        // FIM PREPARA A QUERY

        var stmt: OpaquePointer? = nil

        if sqlite3_open(fileUrl.path, &db) != SQLITE_OK {
            print("Error openning database")
            return false
        }

        if sqlite3_prepare(db,insertQuery,-1,&stmt,nil) != SQLITE_OK {
            print("Error to binding a query")
            return false
        }

        var count : Int32 = 1

        for (key,value) in product {
            print(count)
            if value is String {
                print("\(key) String: \(value)")
                sqlite3_bind_text(db, count, String(describing: value), -1, nil)
            } else if value is Int {
                print("\(key) Int: \(value)")
                sqlite3_bind_int(db, count, Int32(value as! Int))
            } else if value is Float {
                print("\(key) Float: \(value)")
                sqlite3_bind_double(db, count, Double(value as! Double))
            } else if value is [Int] {
                print("\(key) [Int]: \(value)")
                let idsList = value as! [Int]
                var str: String = ""

                for j in idsList {
                    if idsList.last == j {
                        str += String(j)
                    } else {
                        str += String(j) + ","
                    }
                }
                print("Item list: \(str)")
                sqlite3_bind_text(db, count, str, -1, nil)
            } else if value is Bool {
                print("\(key) Bool: \(value)")
                var num : Int32 = 0
                if Bool(value as! Bool) {
                    num = 1
                } else {
                    num = 0
                }
                sqlite3_bind_int(db,count,num)
            }

            count += 1
        }

        if sqlite3_step(stmt) == SQLITE_DONE {
            print("Save successfuly: \(product["display_name"]!)")
            return true
        }

        return false
    }

我的初始化:

static func create() {

    if sqlite3_open(fileUrl.path, &db) != SQLITE_OK {
        print("Error openning database")
        return
    }

    let createTableQuery = "CREATE TABLE IF NOT EXISTS product(id integer primary key," +
        "name text," +
        "default_code text," +
        "destination_type text," +
        "company_ax_id integer," +
        "categ_id integer," +
        "fiscal_class_code text," +
        "taxes_id text," +
        "uom_id integer," +
        "uom_po_id integer," +
        "multiple integer, " +
        "__last_update text, " +
        "display_name text, " +
        "active boolean, " +
        "create_date text, " +
        "create_uid integer, " +
        "currency_id integer," +
        "invoice_police text, " +
        "item_ids text," +
        "list_price text, " +
        "price float," +
        "pricelist_id integer, " +
        "type text);"

    if sqlite3_exec(db,createTableQuery,nil,nil,nil) != SQLITE_OK {
        print("Erro ao criar tabela!")
        return
    }

    print("ProductDB: Banco carregado e tabela pronta!")
}

错误:发生在第一次迭代中。

我确实读过这个:sqlite3.dylib: illegal multi-threaded access to database connection

但不能解决我的问题。我不知道解决方案。我确实尝试关闭并重新打开,也使用sqlite3_open_v2 和其他 v2 选项,但不起作用!请有人帮帮我吗?

【问题讨论】:

  • 每个线程只使用一个连接。

标签: ios swift xcode sqlite


【解决方案1】:

根据:https://www.sqlite.org/threadsafe.html

“多线程。在这种模式下,SQLite 可以安全地被多个线程使用,前提是两个或多个线程中没有同时使用单个数据库连接。”

【讨论】:

  • 意思是每次 CRUD 操作我都要多次使用sqlite3_open
  • 似乎没有其他解决方案,但有人会在我的代码中查看多个 sqlite3_open 语句,他们不会说效率低吗?
猜你喜欢
  • 2018-05-29
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多