gpt4 book ai didi

ios - 使用 SQLite Swift4 时出错 : multi-threaded access to database connection

转载 作者:行者123 更新时间:2023-11-30 11:38:31 25 4
gpt4 key购买 nike

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

[logging] sqlite3.dylib 客户端中的错误:非法多线程访问数据库连接

我的插入功能:

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!")
}

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

enter image description here

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

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

最佳答案

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

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

关于ios - 使用 SQLite Swift4 时出错 : multi-threaded access to database connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49470395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com