sqlite3 select总是返回“ 1”

时间:2018-12-12 04:32:15

标签: ios swift sqlite

我真的是iOS开发的新手。我正在使用Xcode 10.1

我尝试插入,然后从数据库SQLite3中选择所有行,但是对于我调用的每个字段,它总是返回“ 1”。

这是我要插入的代码:

func insertParameter(id : String, group : String, code : String, name : String, parent1 : String, value1 : String, active : String, count : String){
    let db = openDatabase()
    var insertParamDB : OpaquePointer?

    print(id)

    let insertConfig = "Insert or replace into \(TB_Parameter) values (?,?,?,?,?,?,?)"

    if sqlite3_prepare(db, insertConfig, -1, &insertParamDB, nil) != SQLITE_OK{
        print("Error prepare insert param \(String(cString: sqlite3_errmsg(db)))")
        return
    }else{
        if sqlite3_bind_text(insertParamDB, 1, id, -1, nil) != SQLITE_OK{
            print("Error Bind param1 \(String(cString: sqlite3_errmsg(db)))")
            return
        }

        if sqlite3_bind_text(insertParamDB, 2,group, -1, nil) != SQLITE_OK{
            print("Error Bind param2 \(String(cString: sqlite3_errmsg(db)))")
            return
        }

        if sqlite3_bind_text(insertParamDB, 3, code, -1, nil) != SQLITE_OK{
            print("Error Bind param3 \(String(cString: sqlite3_errmsg(db)))")
            return
        }

        if sqlite3_bind_text(insertParamDB, 4, name, -1, nil) != SQLITE_OK{
            print("Error Bind param4 \(String(cString: sqlite3_errmsg(db)))")
            return
        }

        if sqlite3_bind_text(insertParamDB, 5, parent1, -1, nil) != SQLITE_OK{
            print("Error Bind param5 \(String(cString: sqlite3_errmsg(db)))")
            return
        }

        if sqlite3_bind_text(insertParamDB, 6, value1, -1, nil) != SQLITE_OK{
            print("Error Bind param6 \(String(cString: sqlite3_errmsg(db)))")
            return
        }

        if sqlite3_bind_text(insertParamDB, 7, active, -1, nil) != SQLITE_OK{
            print("Error Bind param7 \(String(cString: sqlite3_errmsg(db)))")
            return
        }

        if sqlite3_step(insertParamDB) != SQLITE_DONE{
            print("Error Insert param \(String(cString: sqlite3_errmsg(db)))")
            return
        }

        print(count)
        print("Success Insert Param")
        sqlite3_finalize(insertParamDB)
    }
}

这是我的代码可供阅读

func getallParam()->[SelectQuery]{
    let db = openDatabase()

    var dataParam = [SelectQuery]()
    var readParam : OpaquePointer?
    let getAllParam = "Select \(FD_Param_ID),\(FD_Param_Group),\(FD_Param_Code),\(FD_Param_Name),\(FD_Param_Parent1),\(FD_Param_Value1),\(FD_Param_Active) FROM \(TB_Parameter)"

    if sqlite3_prepare(db, getAllParam, -1, &readParam, nil) != SQLITE_OK {
        print ("Error Prepare get all param \(String(cString: sqlite3_errmsg(db)))")
    }else{

        var i = 0
        while(sqlite3_step(readParam) == SQLITE_ROW) {
            i = i+1
            print(String(i))
            print(String(cString:sqlite3_column_text(readParam, 0)))

            dataParam.append(SelectQuery(id: String(cString:(sqlite3_column_text(readParam, 0)) == nil ? UnsafePointer<UInt8> ("") : sqlite3_column_text(readParam, 0)),
                                         group: String(cString:(sqlite3_column_text(readParam, 1)) == nil ? UnsafePointer<UInt8> ("") : sqlite3_column_text(readParam, 1)),
                                         code: String(cString:(sqlite3_column_text(readParam, 2)) == nil ? UnsafePointer<UInt8> ("") : sqlite3_column_text(readParam, 2)),
                                         name: String(cString:(sqlite3_column_text(readParam, 3)) == nil ? UnsafePointer<UInt8> ("") : sqlite3_column_text(readParam, 3)),
                                         parent1: String(cString:(sqlite3_column_text(readParam, 4)) == nil ? UnsafePointer<UInt8> ("") : sqlite3_column_text(readParam, 4)),
                                         value1: String(cString:(sqlite3_column_text(readParam, 5)) == nil ? UnsafePointer<UInt8> ("") : sqlite3_column_text(readParam, 5)),
                                         active: String(cString:(sqlite3_column_text(readParam, 6)) == nil ? UnsafePointer<UInt8> ("") : sqlite3_column_text(readParam, 6))))
        }

        sqlite3_finalize(readParam)
    }
    return dataParam
}

您可以从我的插入代码中看到我正确打印了从视图控制器接收到的id并正确返回它,并且我从插入代码中成功插入了

您可以从我的读取/选择代码中看到,我打印了列0的值,即ID,但其返回值是“ 1”,当我打印数组时,不仅ID,而且所有字段都返回了“ 1”

我从sqlite tutorial by raysimplified ios学习了这段代码。

也许我想念一些东西,但是有人可以帮助我。

谢谢..

0 个答案:

没有答案