Hi.
I'm in the process of learning Swift coding and decided to attempt a porting of one of my existing Windows programs.
The thing is, that program relies heavily on connecting to a remote MySQL server.

I've seen a few attempts to provide various connectors to an assortment of databases, including MySQL.
Most solutions say that it can be done using a bridge to Object C, but none really show how.

Does anyone here have any experience in connecting a Cocoa/Swift application to a MySQL server (or any remote database at all)?

Dani AI

Generated

Direct connection from a Cocoa/Swift macOS app to a remote MySQL server is entirely feasible — two practical patterns are common: (A) call the native MySQL client (libmysqlclient / Connector/C) from an Objective‑C / Objective‑C++ wrapper and expose that wrapper to Swift via a bridging header, or (B) use a pure‑Swift client library (SwiftNIO‑based drivers / server‑side Swift packages) so no C glue is required. As noted, a web service is the usual safer pattern for mobile clients; for a desktop app the direct approaches below are practical. Importing Objective‑C into Swift · MySQLKit (Vapor). (developer.apple.com)

A concise path for the native C client approach: install the MySQL client dev libraries (Homebrew / system package), add the include and library search paths in Xcode, write a small Objective‑C++ (.mm) wrapper that calls the MySQL C API and returns Foundation types (NSArray/NSDictionary), import the wrapper header in the bridging header, and call it from Swift. Common build failures come from missing header/lib paths or mismatched architectures; the MySQL C API docs and client build notes are the reference for flags and linking. (dev.mysql.com)

An alternative is to add a Swift package (for example MySQLNIO / MySQLKit or server libraries such as Perfect’s MySQL wrapper) and use async/connection‑pooling primitives rather than writing C glue. A tiny Swift call to a wrapper might look like this:

let db = MySQLWrapper()
if db.connect(host:"db.example", user:"joe", password:"pw", db:"app", port:3306) {
    let rows = db.executeQuery("SELECT * FROM customers")
}

For tabular/in‑memory models there is no direct DataSet/DataTable analogue in Cocoa; common choices are Core Data for object‑graph persistence or simple arrays of dictionaries/structs for transient result sets. When connecting directly over the network, enforce TLS, use least‑privilege DB users, prefer prepared statements, and consider SSH/VPN or a backend API if credentials or network security are concerns. Core Data Programming Guide (archive) · [MySQL encrypted connections]. (developer.apple.com)

Recommended Answers

All 4 Replies

Is this a mobile app? If it is you can use sqlite instead, it is supported pretty well in objective c so I guess Swift is the same (haven't got around to updating my iOS knowledge base yet).
Otherwise, if you need to connect to a remote MySQL serve you may have to add another layer, a PHP webservice or similar, that the app connects to and that passes the requests on to the database.
Then you just need to pass info back and forth as JSON and you shouldn't have any issues.

I know that I could use a webservice if this was for iOS (or a webapplication!).
But because I said that I'm porting a Windows program, I would think that it was obvious. Appearently not.
This is for OSX Mavericks and Yosemite, not iOS.

The whole point of creating a program with a database backend, local or remote, is to not have to use webservices.

And while I'm asking.
Is there such a thing as an equivalent of DataSets/DataTables in Cocoa/Swift to make things easier?

Nothing?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.