Home  >  Article  >  php教程  >  Use Swift 3.0 to operate MySQL database

Use Swift 3.0 to operate MySQL database

高洛峰
高洛峰Original
2016-11-22 10:20:441800browse

Start

In this tutorial, we use Ubuntu 16.04 system and MySQL 5.7. MySQL 5.7 introduces a series of new features. One of them is to provide the ability to store JSON data more efficiently, and at the same time provide the ability to query the interior of JSON data. Later, if MySQL 5.7 becomes the default MySQL version on Ubuntu 16.04, we will use Ubuntu 16.04 as our operating system.

If you haven’t installed Swift yet, you can use apt-get to install it. See this article for installation instructions. At the end of September 2016, Apple also began compiling Swift images on Ubuntu 16.04. Please check out Swift.org for more information.

Create database

We named the database swift_test, the assigned user is swift, and the password is swiftpass. If you are familiar with MySQL, you should know that you need to execute GRANT ALL ON swift_test.* for authorization.
The following are the commands for this part:

# sudo mysql
...
mysql> create user swift;
Query OK, 0 rows affected (0.00 sec)

mysql> create database swift_test;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on swift_test.* to 'swift'@'localhost' identified by 'swiftpass';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

Create Swift package

Now start the formal coding, first create a package:

# mkdir swift_mysql
# swift package init --type executable

Write the Package.swift file:

import PackageDescription

let package = Package(
    name: "swift_mysql",
    dependencies:[
      .Package(url:"https://github.com/vapor/mysql", majorVersion:1)
    ]
)

The second step, we use some auxiliary tool code To generate some random data and populate it into the database. Add the utils.swift file under the Sources directory and add the following content inside:

import Glibc

class Random {
  static let initialize:Void = {
    srandom(UInt32(time(nil)))
    return ()
  }()
}

func randomString(ofLength length:Int) -> String {
  Random.initialize
  let charactersString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  let charactersArray:[Character] = Array(charactersString.characters)
  
  var string = ""
  for _ in 0..<length {
    string.append(charactersArray[Int(random()) % charactersArray.count])
  }
               
  return string
}

func randomInt() -> Int {
  Random.initialize
  return Int(random() % 10000)
}

Vapor MySQL

Then comes the real code, our main.swift file uses the Vapor MySQL module.

Connect to the database

Add the following code to Sources/main.swift:

import Glibc
import MySQL

var mysql:Database
do {
  mysql = try Database(host:"localhost",
                       user:"swift",
                       password:"swiftpass",
                       database:"swift_test")
  try mysql.execute("SELECT @@version")
} catch {
  print("Unable to connect to MySQL:  \(error)")
  exit(-1)
}

The above code sets up the database and processes mysql. The constructor Database(host:String, user:String, password:String, database:String) is self-explanatory.
The statement try mysql.execute("SELECT @@version") is used to test to ensure that we are connected correctly and successfully connected to the database. If the do code block runs without errors, you can start operating the database!

Integers and Strings

All calls to MySQL will be through the execute(_:String) method. It should be noted that this method is different from some abstract API methods, such as .create(table:String, ...) or .insert(table:String, .... execute obtains the original SQL statement and passes it to the MySQL connector.

do {
  try mysql.execute("DROP TABLE IF EXISTS foo")
  try mysql.execute("CREATE TABLE foo (bar INT(4), baz VARCHAR(16))")

  for i in 1...10 {
    let int    = randomInt()
    let string = randomString(ofLength:16)
    try mysql.execute("INSERT INTO foo VALUES (\(int), &#39;\(string)&#39;)")
  }

  // Query
  let results = try mysql.execute("SELECT * FROM foo")
  for result in results {
    if let bar = result["bar"]?.int,
       let baz = result["baz"]?.string {
      print("\(bar)\t\(baz)")
    }
  }
} catch {
  print("Error:  \(error)")
  exit(-1)
}

The query result also uses the execute(_:String) method, but the returned result is a [String:Node] dictionary. The key of the dictionary corresponds to the column name of the database.

Node 类型是 Vapor 中的数据结构,用于转化为不同的类型。你可以从这里获取更多的信息。使用 Node 类型来表达 MySQL 可以方便的转换成对应的 Swift 类型。比如:let bar = result["bar"]?.int 给我们一个整型。

继续

接着我们来看一些更复杂的例子,比如创建一个表,包含了 MySQL 的 DATE, POINT 和 JSON 数据类型。我们的表名叫 samples。

do {
  try mysql.execute("DROP TABLE IF EXISTS samples")
  try mysql.execute("CREATE TABLE samples (id INT PRIMARY KEY AUTO_INCREMENT, created_at DATETIME, location POINT, reading JSON)")

  // ... Date
  // ... Point
  // ... Sample
  // ... Insert
  // ... Query
} catch {
  print("Error:  \(error)")
  exit(-1)
}

要插入一个日期到数据库中,需要正确的 SQL 语句:

// ... Date
let now              = Date()
let formatter        = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" // MySQL will accept this format
let created_at       = formatter.string(from:date)

接下来使用 Swift 元组来创建一个 POINT:

// ... Point
let location = (37.20262, -112.98785) // latitude, longitude

最后,我们来处理 MySQL 5.7 中新的 JSON 数据类型,此外我们使用了 Jay 包来快速将一个 Swift 字典 [String:Any] 转换为 JSON 格式的字符串。

// ... Sample
  let sample:[String:Any] = [
    "heading":90,
    "gps":[
      "latitude":37.20262,
      "longitude":-112.98785
    ],
    "speed":82,
    "temperature":200
  ]

提示:你不需要显式在 Package.swift 中声明对 Jay 的依赖,因为在 MySQL 的包中已经包含了这个依赖。接下来我们把 JSON 数据转换为 String,用来拼凑 MySQL 语句。

let sampleData = try Jay(formatting:.minified).dataFromJson(any:sample) // [UInt8]
let sampleJSON = String(data:Data(sampleData), encoding:.utf8)

这样我们就有了 date, point 和 JSON 字符串(sample) 了, 现在添加数据到 sample 表中:

// ... Insert
let stmt = "INSERT INTO samples (created_at, location, sample) VALUES (&#39;\(created_at)&#39;, POINT\(point), &#39;\(sampleJSON)&#39;)"
try mysql.execute(stmt)

请注意我们在处理 POINT 时候,使用了一些技巧。在对 (point) 展开为字符串 (37.20262, -112.98785) 后,完整的字符串是 POINT(37.20262, -112.98785),这是 MySQL 所需要的数据,整个语句的字符串如下:

INSERT INTO samples (created_at, location, sample) VALUES (&#39;2016-09-21 22:28:44&#39;, POINT(37.202620000000003, -112.98784999999999), &#39;{"gps":{"latitude":37.20262,"longitude":-112.98785},"heading":90,"speed":82,"temperature":200}&#39;)

获取结果

警告:在写这篇文章的时候(2016-09-22), Vapor MySQL 1.0.0 有一个 bug:在读取 POINT 数据类型时会 crash 掉,所以不得不在下面代码中加入 do 代码块,然后不使用 select 语句。我们在 Vapor MySQL 中记录了这个 issue,等这个 issue 修复以后,我们将更新文章。

在下面的例子中,我们将使用 MySQL 5.7 中引入对 JSON 数据内部的查询特性,使用 SELECT … WHERE 查询 JSON 数据。在这里查询的是 samples 表中 JSON 数据类型 sample中、speed 字段大于 80 的数据。

// ... 查询
  let results = try mysql.execute("SELECT created_at,sample FROM samples where JSON_EXTRACT(sample, &#39;$.speed&#39;) > 80") 
  for result in results {
    if let sample      = result["sample"]?.object,
       let speed       = sample["speed"]?.int,
       let temperature = sample["temperature"]?.int,
       let created_at  = result["created_at"]?.string {
      print("Time:\(created_at)\tSpeed:\(speed)\tTemperature:\(temperature)")
    }
  }

这里做一些说明。JSON_EXTRACT 函数是用来 返回从 JSON 文档中的数据,根据传入的路径参数选择文档中满足条件的数据。在本例中,我们解包了列 sample 中的 speed 值。

为了循环处理结果,我们使用了 for result in results 语句,接着使用 if let 语句验证结果数据。首先使用 let sample = result["sample"]?.object 获取一个字典,对应 MySQL 中的 JSON 文档,这是一句关键的代码!Vapor MySQL 库并没有返回一个 String,而 String 还需进行 JSON 的解析。这个解析工作库已经帮你做了,所以你可以直接使用 sample 字典啦。

剩下的 let 语句给了我们 speed,temperature 和 created_at。注意 created_at 在 MySQL 中是 DATETIME 类型,我们读取它为字符串。为了在 Swift 中转换成 Date 类型,需要使用 .date(from:String) 方法加一个 DateFormatter 来做类型转换。

获取代码

如果你想直接运行代码,请到 github 上下载我们的代码。
在任何地方使用 swift build 进行编译,运行可执行代码,不要忘了你还需要拥有一个数据库,用户名并且授权通过。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Native php mysql linkNext article:Native php mysql link