使用Perfect - MySQL Connector启用对MySQL数据库的访问

时间:2016-11-01 17:20:37

标签: mysql swift server perfect

我将使用Swift创建Web应用程序。我与MySQL的连接有问题。我使用PhpMyAdmin进行MySQL数据库管理。在我看来,最好的方法是使用完美。我按照Perfect - MySQL Connector完成了所有事情!不幸的是,我在main.swift中调用函数useMysql时遇到问题。我想知道它是否写得正确。我在文件main.swift中调用函数useMysql:

//This route will be used to fetch data from the mysql database
routes.add(method: .get, uri: "/use", handler: useMysql)

然而,在文件mysql_quickstart.swift中,我们使用两个参数定义函数:

public func useMysql(_ request: HTTPRequest, response: HTTPResponse) 
{
//function body 
}

我不确定但也许我应该这样称呼它? -

routes.add(method: .get, uri: "/use", handler: useMysql(request: object of HTTPRequest, response: object of HTTPResponse))

我没有任何错误但是函数useMysql似乎永远不会被执行。我知道从项目文件中附加代码并不是一个好习惯,但也许它会很有用。 Here is my code有什么建议吗?提前谢谢。

我还发现了其他Perfect - MySQL Connector教程,我不知道哪种方式更好?我想知道在一个项目中使用多个框架是否是一个好主意?例如,Perfect和Kitura或Vapor都是?

编辑: 您认为以这种方式使用它会怎样?

// Register your own routes and handlers
var routes = Routes()
routes.add(method: .get, uri: "/", handler: {
        request, response in
        response.setHeader(.contentType, value: "text/html")
        response.appendBody(string: "<html><title>Hello, world!</title><body>Hello, world!</body></html>")
        //This route will be used to fetch data from the mysql database
        useMysql(request, response: response)
        response.completed()
    }
)

2 个答案:

答案 0 :(得分:0)

两件事,用简单的打印结果换出useMysql函数并返回HTML函数。

其次,我建议查看(或使用)MySQL StORM抽象层。

https://github.com/SwiftORM/MySQL-StORM

很可能是它的权限,主机访问权限或端口。

答案 1 :(得分:0)

Main.Swift: -

routes.add(method: .post, uri: "/admin/api/v1/login") { (request, response) in

let email = request.param(name: "email")
let password = request.param(name: "passwrod")
let JSON = Login.checkLogin(email: email, password: password)
do {
    try response.setBody(json: JSON)
        .completed()
} catch {
 }
}

Login.Swift

import Foundation
import PerfectMySQL
import PerfectLib

class Login: NSObject {

 class func checkLogin(email: String?, password: String?)-> JSONConvertible {

 var JSON: JSONConvertible?



 if !ServerHelper.isValidEmail(candidate: email) {

 JSON =  ServerHelper.getErrorJson(message: KMESSAGE_INVALID_EMAIL_ADDRESS)
 return JSON


 } else if !ServerHelper.isValidPassword(password: password!) {

 JSON =  ServerHelper.getErrorJson(message: KMESSAGE_INVALID_PASSWORD)
 return JSON

 } else {


 let mySql = MySQL()
 let connect =  mySql.connect(host: "127.0.0.1", user: "root", password: "mypassword", db:"SuperAdmin" , port: 3306, socket: "", flag: 0)
 if !connect {

 JSON =  ServerHelper.getErrorJson(message: KMESSAGE_SERVER_ERROR)
 return JSON
 }

 let query = "select email, phone_number from SuperAdmin.Login where email='\(String(describing: email!))' AND password='\(String(describing: password!))'"

 if mySql.query(statement: query) {

 let result = mySql.storeResults

 var jsonResult = [Dictionary<String, Any>]()

 result()?.forEachRow(callback: { (element) in

 var dict = Dictionary<String, Any>()

 if let arrRow = element as? [String] {

 let email = arrRow[0]
 let phone_number = arrRow[1]
 dict["email"] = email
 dict["phone_number"] = phone_number
 jsonResult.append(dict)
 }
 })

 if jsonResult.count == 0 {

 JSON =  ServerHelper.getErrorJson(message: KMESSAGE_NOT_AUTHORIZE)

 } else {

 JSON = ServerHelper.getJson(result: jsonResult)

 }

 } else {

 JSON =  ServerHelper.getErrorJson(message: KMESSAGE_NOT_AUTHORIZE)
 return JSON
 }
 mySql.close()
 }
 return JSON
 }


}

点击POST API就好 本地主机:8080 /管理/ API / V1 /登录

我收到了回复:

{
"status": 200,
"succcess": true,
"result": [
    {
        "phone_number": "84747474574",
        "email": "myemail@swift.com"
    }
]


}

请确保您传递正确的值以连接数据库

let connect =  mySql.connect(host: "127.0.0.1", user: "root", password: "mypassword", db:"SuperAdmin" , port: 3306, socket: "", flag: 0)