如何解决此问题数据库错误:1“没有这样的表:用户”?

问题描述 投票:0回答:1
import Foundation
import FMDB

class DBManager {
    static var database = FMDatabase()

    init() {
        config()
    }

    private func config() {
        guard let dbPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("expenses.sqlite") else {
            print("Error: No se pudo obtener la ruta de la base de datos")
            return
        }
        print("Ruta de la base de datos:", dbPath)
        DBManager.database = FMDatabase(url: dbPath)
    }
}
import Foundation
import FMDB

class UserDBManager {

    func fetchUsers() -> [UserModel] {
        var result = [UserModel]()

        guard DBManager.database.open() else {
            print("Error al abrir la base de datos")
            return result
        }
        print("Conexión a la base de datos abierta correctamente")

        let sqlQuery = "SELECT * FROM users"

        do {
            let data = try DBManager.database.executeQuery(sqlQuery, values: nil)
            while data.next() {
                var user = UserModel()
                user.id = Int(data.string(forColumn: "Id") ?? "")
                user.name = data.string(forColumn: "Name") ?? "N/A"
                user.lastName = data.string(forColumn: "Last_Name") ?? "N/A"
                user.email = data.string(forColumn: "Email") ?? "N/A"
                user.password = data.string(forColumn: "Password") ?? "N/A"
                result.append(user)
            }
        } catch let error as NSError {
            print("Error al ejecutar la consulta: \(error.localizedDescription)")
        }

        DBManager.database.close()

        return result
    }

}

我尝试使用此查询

let sqlQuery = "SELECT * FROM users"
从我的数据库中选择所有用户。我正在使用 SQLite (FMDB)。 该应用程序未显示数据,我收到此错误:

没有这样的表:用户

swift sqlite fmdb
1个回答
0
投票

进口粉底 导入FMDB

DBManager 类 {

static var shared = FMDatabase()

private init() {
    
}

static var database : FMDatabase = {
    DBManager.config()
    return DBManager.shared
}()



private static func config() {
    guard let dbPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("expenses.sqlite") else {
        print("Error: No se pudo obtener la ruta de la base de datos")
        return
    }
    DBManager.shared = FMDatabase(url: dbPath)
}

} 我能够用这个配置修复它。我的错误是我没有与负责执行查询的模型共享 func 配置

© www.soinside.com 2019 - 2024. All rights reserved.