OHMySQL can connect to remote or local MySQL database and execute CRUD operations. The framework is built upon MySQL C API, but you don’t need to dive into low-level. The following diagram represents a general architecture. Logic (saving, editing, removing etc.) is aggregated in the app. The database is just a shared storage.
⭐️ Every star is appreciated! ⭐️
- Supports Swift and Objective-C
- Requires minimal knowledge in SQL
- Easy to integrate and use
- Many functionality features
- Up-to-date MySQL library
- Documentation and support
Platform | Supported |
---|---|
iOS | 14.0 |
macOS | 11.0 |
Mac Catalyst | 14.0 |
watchOS | 8.0 |
tvOS | 15.0 |
I wish to support the library and extend API and functionality. If you donate me some money 💵, it keeps me motivated and happy 🙂 You may support me via PayPal or let me know any other method convenient for you.
Read documentation how to install the library as a dependency in your project.
Connect to the database.
let user = MySQLConfiguration(userName: "root",
password: "root",
serverName: "localhost",
dbName: "dbname",
port: 3306,
socket: "/mysql/mysql.sock")
let coordinator = MySQLStoreCoordinator(user: user!)
coordinator.encoding = .UTF8MB4
coordinator.connect()
To end a connection:
coordinator.disconnect()
To execute a query you have to create the context:
let context = MySQLQueryContext()
context.storeCoordinator = coordinator
let dropQueryString = "DROP TABLE `MyTable`"
let dropQueryRequest = MySQLQueryRequest(queryString: dropQueryString)
try? context.execute(dropQueryRequest)
The response contains the array of the dictionaries.
let query = MySQLQueryRequestFactory.select("tasks", condition: nil)
let response = try? context.executeQueryRequestAndFetchResult(query)
let query = MySQLQueryRequestFactory.insert("tasks", set: ["name": "Something", "desctiption": "new task"])
try? context.execute(query)
let query = MySQLQueryRequestFactory.update("tasks", set: ["name": "Something"], condition: "id=7")
try? context.execute(query)
let query = MySQLQueryRequestFactory.delete("tasks", condition: "id=10")
try? context.execute(query)
You can execute 4 types of joins - INNER
, RIGHT
, LEFT
, FULL
.
let query = MySQLQueryRequestFactory.joinType(OHJoinInner, fromTable: "tasks", columnNames: ["id", "name", "description"], joinOn: ["subtasks": "tasks.id=subtasks.parentId"])
let result = try? context.executeQueryRequestAndFetchResult(query)
You have to implement the protocol OHMappingProtocol
for your models.
The library has only a primary logic for mapping, so I would recommend you writing a mapping logic by yourself. If you are using Swift you cannot use fundamental number types (Int
, Double
), only NSNumber
(due to run-time).
context.insertObject(task)
try? context.save()
You can update/delete the objects easily.
let task = Task()
task.name = "sample"
context.updateObject(task)
context.deleteObject(task)
try? context.save()
- If you found a bug, have a suggestion or need help, open the issue.
- If you want to contribute, submit a pull request.
- If you need help, write me.
See LICENSE.