FMDB & Swift, " Optional("no such table: Student info") " 在真实设备中,但它可以在模拟器中完成

FMDB & Swift, " Optional("no such table: Student info") " in real device but it can be done in simulator

我是新手,请帮我解决这个问题,我还有很多其他事情要做,真的谢谢你,非常感谢!

这是 之后的进一步问题 当我在我的设备上执行应用程序并抛出错误时:"no such table: Student info",我打印了所有路径并且它们都指向同一个文件所以我假设数据库已经复制?控制台显示如下:

file:///var/mobile/Containers/Data/Application/B5E42F3C-524E-4BBF-8667-1EED0C963A77/Documents/
file:///var/mobile/Containers/Data/Application/B5E42F3C-524E-4BBF-8667-1EED0C963A77/Documents/Data.db
/var/mobile/Containers/Data/Application/B5E42F3C-524E-4BBF-8667-1EED0C963A77/Documents/Data.db
file:///var/mobile/Containers/Data/Application/B5E42F3C-524E-4BBF-8667-1EED0C963A77/Documents/
file:///var/mobile/Containers/Data/Application/B5E42F3C-524E-4BBF-8667-1EED0C963A77/Documents/Data.db
/var/mobile/Containers/Data/Application/B5E42F3C-524E-4BBF-8667-1EED0C963A77/Documents/Data.db
/var/mobile/Containers/Data/Application/B5E42F3C-524E-4BBF-8667-1EED0C963A77/Documents/Data.db
<NSFileManager: 0x17401c1b0>
2017-03-13 16:43:25.446039 Test1.3[16360:5045427] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-03-13 16:43:25.457278 Test1.3[16360:5045427] [MC] Reading from public effective user settings.
Insert failed:
Optional("no such table: Student info")

Data.db 在我的目标包资源中;我设备中的内容是空白 Data.db

第二个问题:如果你看上一个问题中的Utility.Swift,虽然app在模拟器上运行良好,但加载后,应该有一个alertView说"Your database copy successfully",但是它没有。以下是部分代码:

    class func copyFile(_ fileName: NSString){

    let dbPath: String = getPath(fileName as String)
    let fileManager = FileManager.default

    print(dbPath)
    print(fileManager)

    if !fileManager.fileExists(atPath: dbPath) {
        let documentsURL = Bundle.main.resourceURL
        let fromPath = documentsURL!.appendingPathComponent(fileName as String)

        var error : NSError?
        do {
            try fileManager.copyItem(atPath: fromPath.path, toPath: dbPath)
        }
        catch let error1 as NSError {
            error = error1
        }
        if(error != nil){
            self.invokeAlertMethod("Error Occured", strBody: "\(error?.localizedDescription)" as NSString, delegate: nil)
        }
        else{
            self.invokeAlertMethod("Successed", strBody: "Your database copy successfully", delegate: nil)
        }
    }
} 

首先尝试删除您的应用,然后重新安装。

我在 Swift 中创建了一个基于 FMDB 的项目,您可以使用它来解决您的问题。 FMDB Wrapper class 您也可以在 Objective C 项目中使用。

https://github.com/LalitKY/Swift-FMDB

希望对您有所帮助。

好的,我看过你的演示,回答这个问题。

我发现了几个错误。让我一一过一遍。

1) Your class Utility have a getPath method. What it does it will keep copying db every time although db is already present in documents directory and your documents directory db will be replaced with the sample structure. You should always check that if db is already present in documents directory or not.

2) Your db was getting copied into documents directory but structure wasn't. There was no Student info table in db of documents directory.

3) Please avoid using space or any special characters in table names.

所以我所做的只是更正了实用程序中的方法 getPath class。

请用这个替换你的方法

class func getPath(_ fileName: String) -> String {

         let bundlePath = Bundle.main.path(forResource: "Data", ofType: ".db")
         let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
         let fileManager = FileManager.default
         let fullDestPath = URL(fileURLWithPath: destPath).appendingPathComponent("Data.db")
         if fileManager.fileExists(atPath: fullDestPath.path){
            print("Database file is exist")
            print(fileManager.fileExists(atPath: bundlePath!))
         }else{
            do{
                try fileManager.copyItem(atPath: bundlePath!, toPath: fullDestPath.path)
            }catch{
                print("\n",error)
            }
         }

        print(fullDestPath.path)
        return fullDestPath.path
}

更改这段代码后,我尝试 运行 在我的设备中插入几条记录。

如果您还有其他问题,请告诉我。

如果您觉得这个答案有帮助,请采纳它。