63 |
64 |
SwiftKueryMySQL
65 |
66 |
MySQL plugin for Swift-Kuery framework
67 |
68 |
69 |
70 |
71 | 
72 |
Summary
73 |
74 |
MySQL plugin for the Swift-Kuery framework. It enables you to use Swift-Kuery to manipulate data in a MySQL database.
75 |
Install MySQL
76 |
macOS
77 |
brew install mysql
78 | mysql.server start
79 |
80 |
81 |
Other install options: https://dev.mysql.com/doc/refman/5.7/en/osx-installation.html
82 |
83 |
On macOS, add -Xlinker -L/usr/local/lib to swift commands to point the linker to the MySQL library location.
84 | For example,
85 |
86 | swift build -Xlinker -L/usr/local/lib
87 | swift test -Xlinker -L/usr/local/lib
88 | swift package generate-xcodeproj -Xlinker -L/usr/local/lib
89 |
90 |
Linux
91 |
92 |
Download the release package for your Linux distribution from http://dev.mysql.com/downloads/repo/apt/
93 | For example: wget https://repo.mysql.com//mysql-apt-config_0.8.2-1_all.deb
94 |
95 | sudo dpkg -i mysql-apt-config_0.8.2-1_all.deb
96 | sudo apt-get update
97 | sudo apt-get install mysql-server
98 | sudo service mysql start
99 |
100 | More details: https://dev.mysql.com/doc/refman/5.7/en/linux-installation.html
101 |
102 |
On linux, regular swift commands should work fine:
103 |
104 | swift build
105 | swift test
106 | swift package generate-xcodeproj
107 |
108 |
Using Swift-Kuery-MySQL
109 |
110 |
First create an instance of MySQLConnection by calling:
111 |
let connection = MySQLConnection(host: host, user: user, password: password, database: database,
112 | port: port, characterSet: characterSet)
113 |
114 |
115 |
Where:
116 | - host - hostname or IP of the MySQL server, defaults to localhost
117 | - user - the user name, defaults to current user
118 | - password - the user password, defaults to no password
119 | - database - default database to use if specified
120 | - port - port number for the TCP/IP connection if connecting to server on a non-standard port (not 3306)
121 | - characterSet - MySQL character set to use for the connection
122 |
123 |
All the connection parameters are optional, so if you were using a standard local MySQL server as the current user, you could simply use:
124 | swift
125 | let connection = MySQLConnection(password: password)
126 |
127 | password is also optional, but recommended.
128 |
129 |
Alternatively, call:
130 | swift
131 | let connection = MySQLConnection(url: URL(string: "mysql://\(user):\(password)@\(host):\(port)/\(database)")!))
132 |
133 | You now have a connection that can be used to execute SQL queries created using Swift-Kuery.
134 |
135 |
If you want to share a connection instance between multiple threads use MySQLThreadSafeConnection instead of MySQLConnection:
136 | swift
137 | let connection = MySQLThreadSafeConnection(host: host, user: user, password: password, database: database,
138 | port: port, characterSet: characterSet)
139 |
140 | A MySQLThreadSafeConnection instance can be used to safely execute queries on multiple threads sharing the same connection.
141 |
142 |
To connect to the server and execute a query:
143 | swift
144 | connection.connect() { error in
145 | // if error is nil, connect() was successful
146 | let query = Select(from: table)
147 | connection.execute(query: query) { queryResult in
148 | if let resultSet = queryResult.asResultSet {
149 | for row in resultSet.rows {
150 | // process each row
151 | }
152 | }
153 | }
154 | }
155 |
156 |
157 |
View the Kuery documentation for detailed information on using the Kuery framework.
158 |
License
159 |
160 |
This library is licensed under Apache 2.0. Full license text is available in LICENSE.
161 |
162 |
163 |