This repo is only a first try at a reboot from this great original repository made by @mcorega.
To install this package, simply use the Swift Packages > Add Package Dependency
on your Xcode project.
A connection is the object allowing you to communicate to the server via MySQL queries.
// Create the connection object
let connection = MySQL.Connection(
address: "address",
user: "user",
password: "password",
database: "database" // Optional
)
do{
// Open a new connection
try connection.open()
// Closing the connection
try connection.close()
}
catch {
print("Error: \(error)")
}
Create a new query from a connection.
try connection.exec("USE db")
let results = try connection!.query("SELECT * FROM users")
for row in results.first?.rows ?? [] {
// Do something
}
let results = try connection!.query("SELECT * FROM users; SELECT * FROM projects")
for result in results {
// Do something
}
// Prepare the query
let stmt : MySQL.Statement = try connection.
6D40
prepare("SELECT * FROM table WHERE condition = ?")
// Either execute the query with arguments
stmt.exec(["value"])
// Or execute the query with arguments and get results
let result : Result = try stmt.query(["tableName"])
for row in result.rows {
print(row)
}
// Create a connection pool with 10 connections from connection
let pool = try MySQL.ConnectionPool(connection: connection)
// Use a connection from the connection pool
if let poolConnection = pool.getConnection() {
// Free the connection when done
connPool.free(poolConnection)
}
This project is under MIT license.