swift - 如何在 swift4 中获取 Sqlite3 中的表总数?

标签 swift sqlite

我是 IOS 和 Sqlite3 DB 的新手。

我想在 sqlite3 DB 和 swift4 中实现什么以获得表的总数。

我做了以下步骤。

func getTotalCount()->Int32{
    let queryStatementString = "SELECT COUNT(*) FROM Attendance"
    var stmt: OpaquePointer?

    print("select valid count \(queryStatementString) ")

    var totalCount: Int32 = 0
    var id: Int = 0

    if sqlite3_prepare_v2(db, queryStatementString, -1, &stmt, nil) == SQLITE_OK{
        if sqlite3_step(stmt) == SQLITE_ROW {
            id = Int(sqlite3_column_int(stmt, 0))
            print("id \(id)")
            totalCount = sqlite3_column_count(stmt!)
            print("db: validCount Result:")
            print("validCount \(totalCount)")

        }
    }

    if sqlite3_step(stmt) != SQLITE_DONE {
        let errmsg = String(cString: sqlite3_errmsg(stmt)!)
        print("db: failure getting punchTime: \(errmsg)")

    }

    return totalCount
}

但总是返回 1 作为计数。 我没有找到我想念的地方。

最佳答案

我得到了答案。

我正在检索错误的索引值。

引用下面的代码。

func getTotalCount()->Int32{
    let queryStatementString = "SELECT COUNT(*) FROM Attendance"
    var stmt: OpaquePointer?
    var totalCount: Int32 = 0

    if sqlite3_prepare_v2(db, queryStatementString, -1, &stmt, nil) == SQLITE_OK{
        while sqlite3_step(stmt) == SQLITE_ROW {
            totalCount = sqlite3_column_int(stmt, 0)
            print("totalCount \(totalCount)")
        }
    }

    if sqlite3_step(stmt) != SQLITE_DONE {
        let errmsg = String(cString: sqlite3_errmsg(stmt)!)
        print("db: failure getting punchTime: \(errmsg)")
    }
    return totalCount
}

关于swift - 如何在 swift4 中获取 Sqlite3 中的表总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50886106/

相关文章:

ios - CI - Xcode 9 服务器在集成后不发送电子邮件

php - 使用 Alamofire 将数据保存在 iOS user_id 中

python - Python 中列表的高效持久存储

iphone - 隐秘的sqlite数据库以csv格式?然后将这个csv文件邮寄到iPhone Objective C中

ios - 使用 spritekit 为 2d 平台游戏添加移动平台的轻松进出

iOS - 如何对深层链接进行单元测试以确保它们可以在应用程序端进行处理

ios - 如何将数组的数组放入 Swift 1.2 中的另一个数组

python - python 中的 xml 缺少元素

c - 在sqlite的回调函数中动态重新分配2个dim数组

android - 如何在 Ionic2 中使用预填充的 SQLite 数据库?