ios - FMDB 没有显示任何内容 - swift

标签 ios swift xcode fmdb

我想使用我的本地数据库。 为此,我使用 FMDB .

1.我将pod'FMDB'添加到pod文件,然后pod install

2.我将数据库拖到我的项目中 (kashanmapDB_upgrade_3-4.db)。

enter image description here

3.我创建了一个名为DatabaseManager.swift的类

我的类(class):

import Foundation
import FMDB
class DatabaseManager {

    private let dbFileName = "kashanmapDB_upgrade_3-4.db"
    private var database:FMDatabase!

    init() {
        openDatabase()
    }

    func openDatabase() {

        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
        let dbPath = URL(fileURLWithPath: paths).appendingPathComponent(dbFileName)
        let str_path = Bundle.main.resourceURL!.appendingPathComponent(dbFileName).path
        let database = FMDatabase(path: str_path)

        /* Open database read-only. */
        if (!(database?.open(withFlags: 1))!) {
            print("Could not open database at \(dbPath).")
        } else {
            print("opened database")
            self.database = database;
        }
    }

    func closeDatabase() {
        if (database != nil) {
            database.close()
        }
    }

    func query(queryString:String) {
        if let db = database, let q = db.executeQuery(queryString, withArgumentsIn: nil) {
            while q.next() {
                let data = q.string(forColumn: "id")
                // Do whatever here with fetched data, usually add it to an array and return that array
                print("data a a  \(data)")
            }
        }
    }


}
  • 在我的 Controller 中,我为我的类创建了一个变量并调用我的函数:

      let db = DatabaseManager()
    
     // MARK: - View Lifecycle
     override func viewDidLoad() {
     super.viewDidLoad()
     createPageViewController()
     setupPageControl()
    
      db.openDatabase()
      db.query(queryString: "select * from Tags")
    
    }
    
  • 但我的控制台没有显示任何内容。我的问题出在哪里?

    最佳答案

    尝试以下步骤:

    1. 首先您必须卸载旧应用。

    2. 从 Xcode 以及 finder -> 项目目录中删除旧的“kashanmapDB_upgrade_3-4.db”文件。

    3. 再次复制/添加“kashanmapDB_upgrade_3-4.db”文件,确保选中复制所选项目

                           OR
      
    4. 在 Xcode 中 -> 转到应用程序项目 -> 链接的框架和库 -> 单击“+”按钮将“kashanmapDB_upgrade_3-4.db”文件添加到您的项目

    5. 在 Xcode 项目 - 产品 -> 清理项目中并 然后->构建->运行。

    就这样完成了......!!!

    关于ios - FMDB 没有显示任何内容 - swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42471814/

    相关文章:

    Xcode 使用苹果 native 框架将原始数据从 8 位/ channel 转换为 16 位/ channel

    iphone - 如何使用带有 Interface Builder 的 UINavigationController 创建 UIViewController?

    ios - 使用 Storyboard "UIViewController-ZWG-5q-24I"中的标识符 "Main"实例化 View Controller ,但没有获得 UITableView。

    ios - 变量无法在函数 Swift 外部访问

    ios - fatal error NSArray 元素在 xcode10.1 更新后无法匹配 Swift 数组元素类型

    ios - 如何在 xcode 5 中创建相对约束?

    iphone - 适用于 iOS 4 应用程序的 Xcode 4.2 模板

    ios - Objective-c 从 sqlite 读取图像

    ios - 如何在quickblox中实现发送图像(Swift、iOS、xcode)

    ios - INIntent `setImage` 使运行时在 Swift 5 中崩溃