-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFMDBManager.swift
61 lines (55 loc) · 2.13 KB
/
FMDBManager.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
// DBManager.swift
// Created by Oleksii Kozachenko on 2017-05-03.
// Copyright © 2017 macuser. All rights reserved.
import Foundation
//MARK:- DBManager Class
class DBManager: FMDBManager {
//MARK: Properties
let databaseFileName: String
let pathToDatabase: String
//A FMDatabase object (from the FMDB library) that will be accessing and operating on the actual database.
var database: FMDatabase!
//MARK: Initialization
init(dbFileName: String, inDirectory: URL) {
self.databaseFileName = dbFileName
pathToDatabase = inDirectory.appendingPathComponent(databaseFileName, isDirectory: false).absoluteString
}
func createDatabase() -> Bool {
var created = false
if !FileManager.default.fileExists(atPath: pathToDatabase) {
database = FMDatabase(path: pathToDatabase)
//is creating the database file specified by the initialiser’s argument, if only the file is not found (what we want here actually). No connection is being established at that point though. We just know that after that line we can use the database property to have access to our database.
created = true
}
return created
}
/**
Performs Any SQL statement _(delete, create, update)_
- important:
__Does not__ perform **select** statemtnts.
- parameters:
- withSQLQuery: SQL statement as a string.
- returns:
Bool, indicating the status of the performed operation. True if statement was run successfully, false otherwise.
*/
func editTable(withSQLQuery query: String) -> Bool {
var success = false
if openDatabase() {
do {
try database.executeUpdate(query, values: nil)
success = true
}
catch {
print("Could not edit table:")
print(error.localizedDescription)
print(error)
}
// At the end close the database.
database.close()
}
else {
fatalError("Could not open the database.")
}
return success
}
}