为什么这段代码在查询后没有给出结果:Perfect 2.0 Swift?

时间:2016-09-26 11:19:45

标签: swift perfect

我正在使用Perfect 2.0 - swift将值存储在我使用XAMPP / phpmyadmin创建的Database表中。

但是从swift代码运行查询后。

的结果
mysql.storeResults()

是nill。

但是,我可以看到数据已存储并且查询已成功。

以下是我的代码: -

routes.add(method: .post, uri: "/signup") { (request, response) in

let requestParams = request.postParams
print(request.postParams)
let firstName = request.param(name: "firstName", defaultValue: "")
let lastName = request.param(name: "lastName", defaultValue: "")
let email = request.param(name: "email", defaultValue: "")
let password = request.param(name: "password", defaultValue: "")
let address = request.param(name: "address", defaultValue: "")
let city = request.param(name: "city", defaultValue: "")

var responseData = ""

guard !(firstName?.isEmpty)! else {
    responseData = HelperMethods.createErrorResponse(status: 0, message: "firstName is required!")
    return
}

guard !(lastName?.isEmpty)! else {
    responseData = HelperMethods.createErrorResponse(status: 0, message: "lastName is required!")
    return
}

guard !(email?.isEmpty)! else {
    responseData = HelperMethods.createErrorResponse(status: 0, message: "email is required!")
    return
}

guard !(password?.isEmpty)! else {
    responseData = HelperMethods.createErrorResponse(status: 0, message: "password is required!")
    return
}

guard !(address?.isEmpty)! else {
    responseData = HelperMethods.createErrorResponse(status: 0, message: "address is required!")
    return
}

guard !(city?.isEmpty)! else {
    responseData = HelperMethods.createErrorResponse(status: 0, message: "city is required!")
    return
}


let mysql = MySQL()

let connected = mysql.connect(host: dbHost, user: dbUser, password: dbPassword, db: dbName)

guard connected else {
    responseData = HelperMethods.createErrorResponse(status: 0, message: mysql.errorMessage())
    return
}
defer {
    mysql.close()
    response.appendBody(string: responseData)
    response.completed()
}
var querySuccess = mysql.query(statement: "INSERT INTO Users (firstName,lastName,email,password,address,city) VALUES ('\(firstName!)', '\(lastName!)', '\(email!)', '\(password!)', '\(address!)', '\(city!)' )")

guard querySuccess else {
    responseData = HelperMethods.createErrorResponse(status: 0, message: mysql.errorMessage())
    return
}

guard let results = mysql.storeResults() else {
    responseData = HelperMethods.createErrorResponse(status: 0, message: mysql.errorMessage())
    return
}

var array = [[String:Any]]()

results.forEachRow(callback: { row in
    let optionName = row[0]
    let optionValue = row[1]
    array.append([optionName!:optionValue])
})

responseData = HelperMethods.createSuccessResponse(status: 1, message: "success", data: array)
response.appendBody(string: responseData)
response.completed()


}

0 个答案:

没有答案