【问题标题】:Connecting SQLite 3 Database to Swift将 SQLite 3 数据库连接到 Swift
【发布时间】:2021-02-19 17:35:50
【问题描述】:

我正在尝试在我的 SwiftUI 代码中打开一个 SQLite3 数据库,以访问包中的文件路径,我正在使用以下函数;

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

但是我不断收到错误unable to open database file (code: 14),但在尝试运行几次后,我意识到每次运行代码时path 中的倒数第二个文件夹都不同。如何找到不会更改的捆绑包路径以便代码正常工作?

这些是路径如何变化的一些示例:

倒数第二个文件夹命名为:CAC91DB3-E264-436F-89A8-1E3D76C4DC56

/Library/Developer/CoreSimulator/Devices/EFD14A1B-7207-4840-9ACE-8E44A269CC70/data/Containers/Data/Application/CAC91DB3-E264-436F-89A8-1E3D76C4DC56/Documents

倒数第二个文件夹命名为:0A23051F-9362-4B4D-B49B-BE0F028384DC

/Library/Developer/CoreSimulator/Devices/EFD14A1B-7207-4840-9ACE-8E44A269CC70/data/Containers/Data/Application/0A23051F-9362-4B4D-B49B-BE0F028384DC/Documents

编辑:这是我正在使用的代码:

Import swiftUI
Import Foundation
Import SQLite

func openDatabase() {
    
    let ProductID = Expression<Int64>("ProductID")
    let Name = Expression<String>("Name")
    let Calories = Expression<Double>("Calories")
    let Protein = Expression<Double>("Protein")
    let Carbs = Expression<Double>("Carbs")
    let Fats = Expression<Double>("Fats")
    let Weight = Expression<Double>("Weight")
    let Category = Expression<String>("Category")
    
    
    let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let fileURL = documentDirectory.appendingPathComponent("Database.db")
    
    let db = try! Connection("\(fileURL)")
    
    let Products = Table("Products")
    
   //Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: no such table: Products (code: 1)
    for prID in try! db.prepare(Products) { 
        print("productid: \(prID[ProductID]), name: \(prID[Name]), calories: \(prID[Calories]), protein: \(prID[Protein]), carbs: \(prID[Carbs]), fats: \(prID[Fats]), weight: \(prID[Weight]), category: \(prID[Category])")
        
        DatabaseTest().dataTest.append(Product(name: prID[Name], calories: prID[Calories], protein: prID[Protein], carbs: prID[Carbs], fats: prID[Fats], weight: prID[Weight]))
        
    }
    
}

关于sqlite.swift的澄清参考: (https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md)

【问题讨论】:

  • 您的数据库文件在哪里? Bundle 和 container 是不同的地方。
  • @Asperi 我已将数据库文件集成到项目中,使其在我的计算机中的位置为/Users/diego/Desktop/Bites/Bites/Database

标签: ios swift sqlite swiftui sqlite.swift


【解决方案1】:

这是因为 iOS 中的所有应用程序都是沙盒化的。没有办法避免这种情况。您无法保存完整路径。每次启动应用程序时它都会改变。您需要的是仅保存您的文件名及其父目录。每次您需要访问它们时,都可以使用它们来构建新的文件 URL 和/或路径。

编辑/更新:

如果您的文件位于 App Bundle 中:

let databaseURL = Bundle.main.url(forResource: "Database", withExtension: "db")!

注意:如果您需要数据库路径,则必须获取 fileURL 路径属性,而不是绝对字符串。最后但并非最不重要的 Bundle 是只读的。您需要将数据库移动/复制到另一个目录 (application support) 才能对其进行修改。

let databasePath = databaseURL.path

【讨论】:

  • 我该怎么做?
  • 什么意思?只是不要保存路径。获取文档目录 url let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! 来构造你的 fileURL let fileURL = documentDirectory.appendingPathComponent("yourFile.ext")
  • 我尝试使用此代码,但它说在创建连接后无法在数据库中找到表,这意味着它无法找到数据库并创建了一个新的。我检查了 fileURL 返回的内容,它说expression produced error: error: /var/folders/1f/102gzjts5p96d2_mzkyx27dh0000gn/T/expr5-d55158..swift:1:65: error: cannot find type 'Foundation' in scope Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer&lt;Foundation.URL&gt;(bitPattern: 0x10f8676b0)!.pointee)
  • 您可以编辑您的问题并发布您的代码吗?你的数据库在哪里?
  • 我不会问你是否导入 Foundation,因为 NSSearchPathForDirectoriesInDomains 正在工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 2021-02-26
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多