-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
79 lines (67 loc) · 1.79 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import PerfectMySQL
// manually created testDB in mysql cmd prompt
let testHost = "127.0.0.1"
let testUser = "root"
let testDB = "nadja"
func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
var min = scores[0]
var max = scores[0]
var sum = 0
for score in scores {
if score > max {
max = score
} else if score < min {
min = score
}
sum += score
}
return (min, max, sum)
}
let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9])
print(statistics.sum)
// Prints "120"
print(statistics.2)
// Prints "120"
func fetchData() {
let mysql = MySQL()
let connected = mysql.connect(host: testHost, user: testUser, db: testDB)
guard connected else {
print(mysql.errorMessage())
return
}
defer {
mysql.close()
}
let sql = """
SELECT * FROM interactions;
"""
guard mysql.query(statement: sql) else {
print(mysql.errorMessage())
return
}
let results = mysql.storeResults()!
print(results.numRows())
let insert_sql = """
INSERT INTO interactions (name) VALUES ("transaction_from_swift")
"""
guard mysql.query(statement: insert_sql) else {
print(mysql.errorMessage())
return
}
let select_sql = """
SELECT * FROM interactions;
"""
guard mysql.query(statement: select_sql) else {
print(mysql.errorMessage())
return
}
let select_results = mysql.storeResults()!
print(select_results.numRows())
var ary = [[String:Any]]()
select_results.forEachRow { row in
// reddit.com/r/swift/comments/4rpxh4/print_optional_string_interpolation_without/
ary.append(["name": row[0] ?? "nil"])
}
print("Final Array \(ary)")
}
fetchData()