mysqlswiftserverperfect

Enabling access to MySQL database using Perfect - MySQL Connector


I'm going to create web application using Swift. I have a problem with connection with MySQL. I use PhpMyAdmin for MySQL databases management. It seems to me that the best way is to use Perfect. I have done everything in accordance with Perfect - MySQL Connector! Unfortunately, I have a problem with calling function useMysql in main.swift. I wonder whether it is written correctly. I invoke function useMysql in file main.swift:

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

However in file mysql_quickstart.swift we define function with two arguments:

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

I am not sure but maybe I should call it in this way? -

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

I don't have any errors however function useMysql seems to be never executed. I am aware of fact that it isn't a good habit to attach code from files of project but maybe it will be useful. Here is my code Any suggestions? Thanks in advance.

I have also found other Perfect - MySQL Connector tutorial and I'm not sure which way is better? By the by I wonder if it is a good idea to use more than one framework in one project? For instance both Perfect and Kitura or Vapor, too?

EDITED: What do you think to use it in this way?

// 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()
    }
)

Solution

  • Two things, swap out the useMysql function with a simple print result and return HTML function.

    Second, I suggest looking at (or using) the MySQL StORM abstraction layer.

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

    It's quite possible it's permissions, host access or port.