gpt4 book ai didi

ios - 尝试访问 ios 时,sqlite 预填充数据库为空

转载 作者:行者123 更新时间:2023-11-29 11:46:50 25 4
gpt4 key购买 nike

我是 Swift 3 和 ios 的新手,之前编写过访问数据库的 Android 程序。当前的问题是数据库预填充了 10 个表,但是,当我尝试访问数据库时,模拟器或设备中都没有表,因此无法访问数据。我搜索了论坛和互联网,但只能找到检查数据库是否存在的信息。 sqlite 包装器是正在使用的 FMDB。

代码:

override func viewDidLoad() {
super.viewDidLoad()
let filemgr = FileManager.default
let dirPaths = filemgr.urls(for: .documentDirectory, in: .userDomainMask)

let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!

databasePath = dirPaths[0].appendingPathComponent("level24.db").path

print(databasePath)

let fullDestPath = URL(fileURLWithPath: destPath).appendingPathComponent("level24.db")

let bundleDatabasePath = Bundle.main.path(forResource: "level24", ofType: ".db")

var level2DB = FMDatabase(path: databasePath as String)


if filemgr.fileExists(atPath: fullDestPath.path){
print("Database file is exist")
print(filemgr.fileExists(atPath: bundleDatabasePath!))

}else{
filemgr.replaceItemAt(databasePath, withItemAt: bundleDatabasePath.path)
}



level2DB = FMDatabase(path: databasePath as String)
if (level2DB?.open())!{
print("Database is open")

var querySQL = "SELECT * FROM sqlite_master WHERE name = '\(tblName)' and type='table'"
let results:FMResultSet? = level2DB?.executeQuery(querySQL, withArgumentsIn:nil)

if (results == nil){
print("Results = \(results)")

do{
try filemgr.copyItem(atPath: bundleDatabasePath!, toPath: fullDestPath.path)
}catch{
print("\n",error)
}

}
print("Results after = \(results)")

let querySQL2 = "SELECT QUESTION FROM tblSani WHERE _ID = 5"

let results2:FMResultSet? = level2DB?.executeQuery(querySQL2, withArgumentsIn:nil)

print(results2?.string(forColumn: "QUESTION") as Any)

}
}

输出是:

 /Users/***/Library/Developer/CoreSimulator/Devices/4F511422-2F86-49BF-AB10-5CA74B9A7B40/data/Containers/Data/Application/58DD87EB-C20F-4058-B9BC-CCD7ECEEFA98/Documents/level24.db
Database is open
Results after = Optional(<FMResultSet: 0x608000245220>)
2017-04-05 21:09:26.833 Level 2[3161:210849] DB Error: 1 "no such table: tblSani"
2017-04-05 21:09:26.833 Level 2[3161:210849] DB Query: SELECT QUESTION FROM tblSani WHERE _ID = 5
2017-04-05 21:09:26.834 Level 2[3161:210849]DBPath: /Users/***/Library/Developer/CoreSimulator/Devices/4F511422-2F86-49BF-AB10-5CA74B9A7B40/data/Containers/Data/Application/58DD87EB-C20F-4058-B9BC-CCD7ECEEFA98/Documents/level24.db
nil

如果您能帮助我找到解决问题的方法,我们将不胜感激。

最佳答案

经过数小时的努力,我想出了这个解决方案:这被放入 AppDelegate.swift 并将在应用程序启动时检查

代码:

var filemgr = FileManager.default
static let dirPaths = FileManager().urls(for: .documentDirectory, in: .userDomainMask)
static let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!

var databasePath = dirPaths[0].appendingPathComponent("level24.db").path

static let fullDestPath = URL(fileURLWithPath: destPath).appendingPathComponent("level24.db")

static let bundleDatabasePath = Bundle.main.path(forResource: "level24", ofType: ".db")

//function to check if dbase exists
func checkdbase(){

print(databasePath)

print("Full path = \(AppDelegate.fullDestPath)")

var level2DB = FMDatabase(path: databasePath as String)


if filemgr.fileExists(atPath: AppDelegate.fullDestPath.path){

print("Database file is exist")
print(filemgr.fileExists(atPath: AppDelegate.bundleDatabasePath!))
print("bundle = \(AppDelegate.bundleDatabasePath)")

let level2DB = FMDatabase(path: databasePath as String)

if (level2DB?.open())!{
print("Database is open")

// use a select statement for a known table and row
let querySQL2 = "SELECT * FROM tblSani WHERE _ID = 5"

let results:FMResultSet? = level2DB?.executeQuery(querySQL2, withArgumentsIn:nil)

if results?.next()==true{
print("Database has tables")

}else{
print("Database no tables")
removeDB()

}

}
}else{
removeDB()
}
}

//function to remove existing dbase then unpack the dbase from the bundle
func removeDB(){

do{
try filemgr.removeItem(atPath: AppDelegate.fullDestPath.path)
print("Database removed")

}catch {
NSLog("ERROR deleting file: \(AppDelegate.fullDestPath)")
}

do{
try filemgr.copyItem(atPath: AppDelegate.bundleDatabasePath!, toPath: AppDelegate.fullDestPath.path)
print("Databse re-copied")
}catch{
print("\n",error)
}

}

从 AppDelegate 'didFinishLaunchingWithOptions' 调用函数

checkdbase()

如果你能改进这个答案,请帮助其他可能有同样问题的人

关于ios - 尝试访问 ios 时,sqlite 预填充数据库为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43294099/

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