我将使用 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 似乎从未执行过。我知道从项目文件中附加代码不是一个好习惯,但也许它会很有用。 这是我的代码有什么建议吗?提前致谢。
我还找到了其他 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()
}
)