gpt4 book ai didi

ios - 错误域=FM数据库代码=8 "attempt to write a readonly database"

转载 作者:行者123 更新时间:2023-11-28 07:59:42 24 4
gpt4 key购买 nike

我正在使用 FMDatabase 库来使用准备好的数据库 sqlite。

我遇到了这个错误:

Error Domain=FMDatabase Code=8 "attempt to write a readonly database" UserInfo={NSLocalizedDescription=attempt to write a readonly database}
2017-10-27 19:59:10.983238+0330 kashanmap[417:63718] Unknown error calling sqlite3_step (8: attempt to write a readonly database) eu
2017-10-27 19:59:10.983473+0330 kashanmap[417:63718] DB Query: insert into LocationInfoFa

这是我的课:

import FMDB
class DatabaseManager {

private let dbFileName = "kashanmapDB_upgrade_3-4.db"
private var database:FMDatabase!

let TABLE_LOCATION_FA = "LocationInfoFa";
let TABLE_LOCATION_EN = "LocationInfoEn";
let TABLE_GREAT_PEOPLE_FA = "GreatPeopleInfoFa";
let TABLE_GREAT_PEOPLE_EN = "GreatPeopleInfoEn";
let TABLE_TAGS = "Tags";
let TABLE_RELATION_TAG_LOCATION = "RelationTagLocation";
let TABLE_NECESSARY_INFORMATION = "NecessaryInformation";
let TABLE_SLIDER_FA = "SliderFa";
let TABLE_SLIDER_EN = "SliderEn";
let DATABASE_VERSION = 4;
static var LANGUAGE = 1 ; //1:Fa , 2:En
var utilities = Utilities()

init() {
openDatabase()

if(utilities.getData(key: "lang") == "2")
{
DatabaseManager.LANGUAGE = 2
}

}

func openDatabase() {

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let dbPath = URL(fileURLWithPath: paths).appendingPathComponent(dbFileName)
let str_path = Bundle.main.resourceURL!.appendingPathComponent(dbFileName).path
let database = FMDatabase(path: str_path)

/* Open database read-only. */
if (!(database.open(withFlags: 2))) {
print("Could not open database at \(dbPath).")
} else {
print("opened database")
self.database = database;
}
}

func closeDatabase() {
if (database != nil) {
database.close()
}
}

我的数据库路径:

enter image description here

我的查询:

do {
let db = database

let q = try db?.executeUpdate("insert into \(table) (catid,subcat_id,id,subcat_title,title,description,lat,lon,takhfif,images,wifi,apple_health,wc,full_time,pos,work_hours,phone,mobile,fax,website,email,address,facebook,instagram,linkedin,telegram,googleplus,twitter,publish,feature,manager,city,rating_sum,rate_count,lastip,parking,isMallID,mallID,discount_images,price_images,newProduct_images,services_images,order_online,out_upon,cat_title,cat_icon,last_modify,item_logo,cat_logo,rate_sum1,rate_sum2,rate_sum3,rate_count1,rate_count2,rate_count3,rate_title1,rate_title2,rate_title3,rate_enable,installments_text,installments_image) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", values:[ catid,subcat_id,id,subcat_title,title,description,lat,lon,takhfif,images,wifi,apple_health,wc,full_time,pos,work_hours,phone,mobile,fax,website,email,address,facebook,instagram,linkedin,telegram,googleplus,twitter,publish,feature,manager,city,rating_sum,rate_count,lastip,parking,isMallID,mallID,discount_images,price_images,newProduct_images,services_images,order_online,out_upon,cat_title,cat_icon,last_modify,item_logo,cat_logo,rate_sum1,rate_sum2,rate_sum3,rate_count1,rate_count2,rate_count3,rate_title1,rate_title2,rate_title3,rate_enable,installments_text,installments_image])

} catch {
print("\(error)")
}

堆栈溢出有一些解决方案,但它们不被接受为真正的答案。

已更新2

我遇到了这个错误:

DB does not exist in documents folder

我的代码:

   init() {
openDatabase()

if(utilities.getData(key: "lang") == "2")
{
DatabaseManager.LANGUAGE = 2
}

}

func copyDatabaseIfNeeded() {
// Move database file from bundle to documents folder

let fileManager = FileManager.default

let documentsUrl = fileManager.urls(for: .documentDirectory,
in: .userDomainMask)

guard documentsUrl.count != 0 else {
return // Could not find documents URL
}

let finalDatabaseURL = documentsUrl.first!.appendingPathComponent("kashanmapDB_upgrade_3-4.db")

if !( (try? finalDatabaseURL.checkResourceIsReachable()) ?? false) {
print("DB does not exist in documents folder")

let documentsURL = Bundle.main.resourceURL?.appendingPathComponent("kashanmapDB_upgrade_3-4.db")

do {
try fileManager.copyItem(atPath: (documentsURL?.path)!, toPath: finalDatabaseURL.path)
} catch let error as NSError {
print("Couldn't copy file to final location! Error:\(error.description)")
}

} else {
print("Database file found at path: \(finalDatabaseURL.path)")
}

}

func openDatabase() {

self.copyDatabaseIfNeeded()

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let dbPath = URL(fileURLWithPath: paths).appendingPathComponent(dbFileName)
let str_path = Bundle.main.resourceURL!.appendingPathComponent(dbFileName).path
let database = FMDatabase(path: str_path)

/* Open database read-only. */
if (!(database.open(withFlags: 2))) {
print("Could not open database at \(dbPath).")
} else {
print("opened database")
self.database = database;
}
}

最佳答案

您正面临此错误,因为您正在尝试写入(更新)bundle 目录中的 .db 文件,不允许 :

AppName.app:

This is the app’s bundle. This directory contains the app and all ofits resources. You cannot write to this directory. To preventtampering, the bundle directory is signed at installation time.Writing to this directory changes the signature and prevents your appfrom launching. You can, however, gain read-only access to anyresources stored in the apps bundle.

File System Basics - Table 1-1: Commonly used directories of an iOS app

如果你打算更新文件,你应该实现一个逻辑来复制它——如果它之前还没有被复制——到文档目录,因此您将能够使用复制的文件读取/写入事务。

请注意,对于 iOS 11 及更高版本,如果您不希望让最终用户通过文件 iOS 应用程序导航到您的应用程序时。详情请查看iOS Storage Best Practices Apple视频 session 。

你会注意到这个逻辑应该应用于应用程序主包中的任何文件,例如它也是 applicable for JSON files .

关于ios - 错误域=FM数据库代码=8 "attempt to write a readonly database",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46979903/

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