使用 Perfect - MySQL 连接器启用对 MySQL 数据库的访问
Enabling access to MySQL database using Perfect - MySQL Connector
我将使用 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()
}
)
两件事,用简单的打印结果和 return HTML 函数交换 useMysql 函数。
其次,我建议查看(或使用)MySQL StORM 抽象层。
https://github.com/SwiftORM/MySQL-StORM
很有可能是权限、主机访问或端口。
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
}
}
点击POSTAPI赞
localhost: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)
我将使用 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()
}
)
两件事,用简单的打印结果和 return HTML 函数交换 useMysql 函数。
其次,我建议查看(或使用)MySQL StORM 抽象层。
https://github.com/SwiftORM/MySQL-StORM
很有可能是权限、主机访问或端口。
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
}
}
点击POSTAPI赞 localhost: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)