热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

即使数据库FMDBSwift中有数据,结果集也返回nil-ResultsetreturningnileventhereisdataindatabaseFMDBSwift

HelloIamusingFMDBintomyswiftproject.Icopiedmysqitefileintodocumentsdirectoryandit

Hello I am using FMDB into my swift project. I copied my sqite file into documents directory and it success. I can get sqlite file which is copied into documents directory folder. I am inserting data into db as this:

你好我在我的swift项目中使用FMDB。我将sqite文件复制到文档目录中并成功。我可以将sqlite文件复制到文件目录文件夹中。我将数据插入数据库,如下所示:

if db.open() {
    let InsertQry = "insert into profiles (image_url, name,dob,gender) values ('\(imgName)','\(nameTxt.text!)','\(dobTxt.text!)','\(genderTxt.text!)')"
    print("query= \(InsertQry)")
    let result = db.executeUpdate(InsertQry, withArgumentsInArray: nil)
    if !result {
        print(db.lastErrorMessage())
    }
    else
    {
         print("Success")
    }
 }

And it insert data successfully I can see data in sqlite file using sqlite manager(Firefox Add-One plugin). Now I want to get all data from my table using this code:

它成功插入数据我可以使用sqlite manager(Firefox Add-One插件)在sqlite文件中查看数据。现在我想使用以下代码从我的表中获取所有数据:

if db.open() {
    let querySQL = "SELECT * FROM profiles"

    let results:FMResultSet? = self.db.executeQuery(querySQL,withArgumentsInArray: nil)
    if results?.next() == true {
       self.userProfilesArray = NSMutableArray()
       while results!.next() {
            let profileInfo = NSMutableDictionary()
           profileInfo.setObject(results!.stringForColumn("image_url"), forKey: "image_url")
            profileInfo.setObject(results!.stringForColumn("name"), forKey: "name")
            profileInfo.setObject(results!.stringForColumn("dob"), forKey: "dob")
            profileInfo.setObject(results!.stringForColumn("gender"), forKey: "gender")
            self.userProfilesArray.addObject(profileInfo) 
        }
     } else {
         print("results nil")
      }
    db.close()
} 

Issue is I am going into if results?.next() == true statement but I am not going to while loop and I am not getting data even there is data in my databse.

问题是我要进入结果?.next()== true语句但是我不打算循环,即使我的数据库中有数据,我也没有得到数据。

1 个解决方案

#1


1  

Each call to next() consumes a row. If your table contains a single row, your while loop won't see it because the if statement consumes it.

每次调用next()都会消耗一行。如果您的表包含单行,则while循环将看不到它,因为if语句使用它。

So replace if results?.next() == true { with if let results = results {. This will even turn the print("results nil") line into an accurate line.

那么替换if结果?.next()== true {with if let results = results {。这甚至可以将打印(“结果为零”)线转换为准确的线。

After you have ported your existing Objective-C code to Swift, consider using a real Swift SQLite library: they expose result sets as regular collection, handle errors in the Swift way, and are much easier to use. See for example https://github.com/groue/GRDB.swift#fetching-rows

在将现有的Objective-C代码移植到Swift之后,请考虑使用真正的Swift SQLite库:它们将结果集公开为常规集合,以Swift方式处理错误,并且更易于使用。请参阅https://github.com/groue/GRDB.swift#fetching-rows


推荐阅读
author-avatar
革启兵
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有