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

标签 mysql swift server perfect

我将使用 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 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()
    }
)

最佳答案

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

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

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

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

关于mysql - 使用 Perfect - MySQL Connector 启用对 MySQL 数据库的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40365250/

相关文章:

php - 起点和终点列表的基本非加权寻路

swift - 如何关闭 UIView 上的 SFSafariViewController

swift - cellTopLabel背景颜色和cornerRadius

linux - 需要选择什么样的服务器

php 从 mysql 数据库编码 JSON,但显示 "???"数据

mysql - MySQL数据库结构帮助-用户通知系统

php - 提交表单后无法获取变量

swift - RxBluetoothKit 文档?

http - 为什么 http.Get 会产生两个请求而不是一个请求?

php - 将网站从 Windows 本地主机移动到 Linux 实时服务器 : issues with filenames and paths