Swift 5.1 + MySql + macOS

Hello to all,

I am creating a macOS XCode app using Swift 5.1 which should run locally on Mac. I already have a MySql database with data from a Java application.

How can I make the connection and access the data through XCode application?

I saw several articles but all talk about app running in a web environment. In my case it is a local app.

Thanks.

Accepted Reply

Replies

That's a bit general question. It depends what you want to do exactly.

To retrieve data, sending post requests to PHP scripts on the server may be enough.


I propose you first look at :

h ttps://www.reddit.com/r/swift/comments/56jn0p/how_to_connect_swift_to_mysql/


and :

h ttps://github.com/PerfectlySoft/Perfect-MySQL


Hope that helps

Claude31,

Thanks for the answer.

The app will be local on Mac, reading and showing data on noteook, running only Swift, without PHP.

Is it necessary to have a PHP interface to use MySql?

Or is there any other database better suited for use with XCode / Swift?

Not absolutely necessay but very convenient. That provide some API to access data.

You may want to connect your macOS app directly to the MySQL server. Then you need to find a client library of MySQL for macOS.


You can find some open sourced Swift version on the web, but I do not know if they are reliable enough.


Some mysql package for macOS may include libmysqlclient for macOS, but libmysqlclient is a C-API which is hard to use with Swift.


---

You can build some REST-API server on your local environment in PHP, or, if you prefer, in Java. And access it through HTTP.


Which do you prefer?

There’s two approaches you can take here:

  • Talk to the database via a web service, for example, Claude31’s PHP suggestion (A)

  • Talk to the database directly via a client library (B)

It sounds like you want to do B. That’s relatively unusual to do in a client app, so if you search around on the ’net you’ll mostly find suggestions for A.

OTOH, doing B is quite common on the server side, so my recommendation is that you focus your search in the Swift on server space. For example, I popped over to the Server area on Swift Forums, and immediately found this.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

ps DTS is closed 21 Dec through 1 Jan.

Tks, eskimo,

I'm studing option with PHP.

I had the same problem; I ended up writing a simple server script using node js packages which has both a http server package and mysql package (and file system package) so the lines of code was about 120; this included the ability to send binary image and pdf files back also which was necessary for my application. The mysql result sets are sent back as JSON dictionary collections which can be easily parsed on the client side into appropriate data model classes. (There were some issues with accessing files if the file path on the server contains & characters so this also added some code on both client and server).

I am having the same issue. Years ago, i was able to directly talk to a remote DB from a Windows desktop app. I am shocked that this is not easier to do on a Mac. Any solutions?

If you want to connect to MySQL database directly from your app, you may choose the library OHMySQL. It supports iOS, macOS and Mac Catalyst. Moreover, it has been recently updated to the latest mysqlclient v8.0.27.

Enjoy!