How to use FMDB on the generic iOS device instead of simulator?

淺唱寂寞╮ 提交于 2019-12-11 04:43:40

问题


I've created an app using Swift3 and Xcode8, and using FMDB as my database, when it runs on the simulator it can get the data from data.db,but when it runs on the generic device (which is my phone), there's no data in the tableView, also could't insert records. I added data.db into my project, but when I changed records on simulator, records in data.db didn't change, but I printed the path, it pointed to simulator folder, database in that folder will changed along the modify in simulator and that folder's path changes almost every time. I'm so confused, is that because I didn't connected to my database in fact?

Here's the Utility.Swift which holds common and often reused function

import UIKit

class Utility: NSObject {

class func getPath(_ fileName: String) -> String {
    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let fileURL = documentsURL.appendingPathComponent(fileName)
    print(fileURL.path)
    return fileURL.path
    }

class func copyFile(_ fileName: NSString){

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

    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
        }
        let alert: UIAlertView = UIAlertView()
        if (error != nil) {
            alert.title = "Error Occured"
            alert.message = error?.localizedDescription
        }
        else {
            alert.title = "Successfully Copy"
            alert.message = "Your database copy successfully"
        }
        alert.delegate = nil
        alert.addButton(withTitle: "Ok")
        alert.show()
        }
    }

class func invokeAlertMethod(_ strTitle: NSString, strBody: NSString, delegate: AnyObject?) {
    let alert: UIAlertView = UIAlertView()
    alert.message = strBody as String
    alert.title = strTitle as String
    alert.delegate = delegate
    alert.addButton(withTitle: "Ok")
    alert.show()
    }  
}

and StudentDataBase.swift contains Query languages

import UIKit
let sharedInstance = StudentDataBase()

class StudentDataBase : NSObject {

var database: FMDatabase? = nil

class func getInstance() -> StudentDataBase{

    if((sharedInstance.database) == nil)
    {
        sharedInstance.database = FMDatabase(path: Utility.getPath("data.db"))
    }
    return sharedInstance
}


func addStuData(_ student: Student) -> Bool{

    sharedInstance.database!.open()

    let isInserted = sharedInstance.database!.executeUpdate("INSERT INTO [Student info] (StudentID, FirstName, LastName, PhoneNumber) VALUES (?, ?, ?, ?)", withArgumentsIn: [student.studentID, student.fstName, student.lstName, student.phoneNum])

    sharedInstance.database!.close()
    return isInserted
}


func updateStuData(_ student: Student) -> Bool {

    sharedInstance.database!.open()

    let isUpdated = sharedInstance.database!.executeUpdate("UPDATE [Student info] SET FirstName=?, LastName=?, PhoneNumber=? WHERE StudentID=?", withArgumentsIn: [student.fstName, student.lstName, student.phoneNum, student.studentID])
    print(student)
    print(isUpdated)
    sharedInstance.database!.close()
    return isUpdated
}


func deleteStuData(_ student: Student) -> Bool {

    sharedInstance.database!.open()

    let isDeleted = sharedInstance.database!.executeUpdate("DELETE FROM [Student info] WHERE StudentID=?", withArgumentsIn: [student.studentID])

    sharedInstance.database!.close()
    return isDeleted
}

func getAllStuData() -> [Student] {

    sharedInstance.database!.open()

    let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM [Student info]", withArgumentsIn: nil)
    var marrStudentInfo : [Student] = []
    if (resultSet != nil) {
        while resultSet.next() {
            let student : Student = Student()
            student.studentID = resultSet.string(forColumn: "StudentID")
            student.fstName = resultSet.string(forColumn: "FirstName")
            student.lstName = resultSet.string(forColumn: "LastName")
            student.phoneNum = resultSet.string(forColumn: "PhoneNumber")
            marrStudentInfo.append(student)
        }
    }
    sharedInstance.database!.close()
    return marrStudentInfo
}
}

also in AppDelegate.swift, I've written:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    Utility.copyFile("data.db")
    return true
}

I'm new to Swift and FMDB, please explain and I'll try my best.Thank you very much!!!!

Edit: After I downloaded the contents in my phone and the database is blank. And after I throwaway the error, it shows

Inserted failed:Optional("no such table: Student info")

回答1:


SQLite offers good error reporting and you should avail yourself of it. So, for example, if any of these executeUpdate calls fail, you should examine the lastErrorMessage (before you close the database) so you know why it failed.

Personally, I'd suggest using executeQuery(_:values:) and executeUpdate(_:values:) rather than executeQuery(_:withArgumentsIn:) and executeUpdate(_:withArgumentsIn:) because the former methods throw errors (which shifts to a more active error handling paradigm from the more passive one in which you have to manually remember to check for errors). But regardless of how you do your error handling, do it, or else we're all guessing.

Personally, I'd suggest using Xcode's "Devices" window and download your app's bundle (see https://stackoverflow.com/a/38064225/1271826) and look at the database. I bet it's a blank database with no tables defined at all. In terms of why, it could be because you did some earlier testing and there's an old version of the database in the Documents folder. Try completely deleting the app from the device and re-running and see if that fixes it.

Other, more obscure sources of problem include filename capitalization (e.g. Data.db vs data.db). The macOS file system will often handle that gracefully because it's not (generally) case sensitive. But iOS devices are case sensitive.

But, like I said, we're flying blind until (a) you add some error handling to your code so you can see why its failing; and (b) look at the database on the device to see what it looks like.



来源:https://stackoverflow.com/questions/42653293/how-to-use-fmdb-on-the-generic-ios-device-instead-of-simulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!