【问题标题】:Enabling access to MySQL database using Perfect - MySQL Connector使用 Perfect - MySQL 连接器启用对 MySQL 数据库的访问
【发布时间】:2016-11-01 17:20:37
【问题描述】:

我将使用 Swift 创建 Web 应用程序。我与 MySQL 的连接有问题。我使用 PhpMyAdmin 进行 MySQL 数据库管理。在我看来,最好的方法是使用 Perfect。我已经按照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 连接器教程,但我不确定哪种方式更好?顺便说一句,我想知道在一个项目中使用多个框架是否是个好主意?例如 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()
    }
)

【问题讨论】:

    标签: mysql swift server perfect


    【解决方案1】:

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

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

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

    很可能是权限、主机访问或端口。

    【讨论】:

      【解决方案2】:

      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/admin/api/v1/login

      我得到了回复:

      {
      "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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 2014-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多