我需要在我的数据库中保存大部分数据,并收到以下错误:
[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选项,不行!请有人帮帮我吗?
答案 0 :(得分:0)
根据:https://www.sqlite.org/threadsafe.html
“多线程。在这种模式下,只要两个或多个线程中没有同时使用单个数据库连接,SQLite可以安全地被多个线程使用。”