Skip to content

Commit 6b8d0ce

Browse files
committed
Publish v0.8.1
1 parent 965c86e commit 6b8d0ce

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

README.md

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ To install SQLiteAdapter using [Swift Package Manager](https://swift.org/package
1414

1515
```swift
1616
dependencies: [
17-
.package(url: "https://github.com/denissimon/SQLiteAdapter.git", from: "0.8.0")
17+
.package(url: "https://github.com/denissimon/SQLiteAdapter.git", from: "0.8.1")
1818
]
1919
```
2020

2121
Or through Xcode:
2222

2323
```txt
24-
File -> Add Packages
24+
File -> Add Package Dependencies
2525
Enter Package URL: https://github.com/denissimon/SQLiteAdapter
2626
```
2727

@@ -53,13 +53,17 @@ Usage
5353
```swift
5454
import SQLiteAdapter
5555

56-
let dbPath = try! (FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
57-
.appendingPathComponent("db.sqlite")).path // the sqlite file will be created if it does not already exist
58-
let sqlite = try? SQLite(path: dbPath) // with 'recreate: true', the sqlite file will be deleted and recreated
56+
// The sqlite file will be created if it does not already exist
57+
guard let dbPath = try? (FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
58+
.appendingPathComponent("db.sqlite")).path else { return }
59+
60+
guard let sqlite = try? SQLite(path: dbPath) else { return }
5961

6062
print(sqlite.dbPath) // -> path of the sqlite file
6163
```
6264

65+
When initializing `SQLite` with `recreate: true`, the sqlite file will be deleted and recreated.
66+
6367
### Model and create a table
6468

6569
```swift
@@ -84,43 +88,51 @@ let statementCreateTable = """
8488
);
8589
"""
8690

87-
try? sqlite?.createTable(sql: statementCreateTable)
91+
try? sqlite.createTable(sql: statementCreateTable)
8892
```
8993

9094
### SQL operations
9195

96+
Some examples of SQL operations:
97+
9298
```swift
9399
do {
94100
var sql = "INSERT INTO \(sqlTable.name) (json, updated) VALUES (?, ?);"
95101
try sqlite.insertRow(sql: sql, params: ["someJson", Date()])
96102

97-
sql = "INSERT INTO \(sqlTable.name) (json, updated) VALUES (?, ?), (?, ?);"
98-
let (changes, lastInsertID) = try sqlite.insertRow(sql: sql, params: [nil, Date(), nil, Date()])
99-
assert((changes, lastInsertID) == (2, 3))
103+
sql = "INSERT INTO \(sqlTable.name) (json, updated) VALUES (?, ?), (?, ?), (?, ?);"
104+
let date = Date()
105+
let (changes, lastInsertID) = try sqlite.insertRow(sql: sql, params: [nil, date, nil, date, nil, date])
106+
assert((changes, lastInsertID) == (3, 4))
100107

101108
sql = "UPDATE \(sqlTable.name) SET isDeleted = ?, updated = ? WHERE \(sqlTable.primaryKey) IN (2, 3)"
102109
try sqlite.updateRow(sql: sql, params: [true, Date()])
103110
assert(sqlite.changes == 2)
104111

105112
sql = "SELECT * FROM \(sqlTable.name) WHERE isDeleted = ?"
106-
if let rows = try sqlite.getRow(from: sqlTable, sql: sql, params: [true])
113+
if let rows = try sqlite.getRow(from: sqlTable, sql: sql, params: [true]) {
107114
assert(rows.count == 2)
108115
}
109116

110117
try sqlite.deleteByID(in: sqlTable, id: 2)
111118
try sqlite.deleteByID(in: sqlTable, id: 3)
112119

113120
let rowCount = try sqlite.getRowCount(in: sqlTable)
114-
assert(rowCount == 1)
121+
assert(rowCount == 2)
115122

116123
if let row = try sqlite.getFirstRow(from: sqlTable) {
117-
assert(row[0].value as! Int == 1) // "id" INTEGER NOT NULL
118-
assert(row[1].value as? String == "someJson") // "json" TEXT NULL
119-
assert(row[2].value as! Bool == false) // "isDeleted" BOOLEAN DEFAULT 0 NOT NULL
120-
assert((row[3].value as! Date) <= Date()) // "updated" DATETIME NOT NULL
124+
assert(row[0].value as! Int == 1) // "id"
125+
assert(row[1].value as? String == "someJson") // "json"
126+
assert(row[2].value as! Bool == false) // "isDeleted"
127+
}
128+
129+
if let row = try sqlite.getLastRow(from: sqlTable) {
130+
assert(row[0].value as! Int == 4) // "id"
131+
assert(row[1].value as? String == nil) // "json"
132+
assert(row[2].value as! Bool == false) // "isDeleted"
121133
}
122134
} catch {
123-
print("SQLite:", error)
135+
print(error.localizedDescription)
124136
}
125137
```
126138

@@ -133,7 +145,7 @@ let row = try sqlite.getByID(from: sqlTable, id: 10) // -> nil
133145
Insert, update, and delete methods return the number of changes made:
134146

135147
```swift
136-
let changes = try sqlite.deleteAllRows(in: sqlTable) // -> 1
148+
let changes = try sqlite.deleteAllRows(in: sqlTable) // -> 2
137149
```
138150

139151
### Optional settings

SQLiteAdapter.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'SQLiteAdapter'
3-
s.version = '0.8.0'
3+
s.version = '0.8.1'
44
s.homepage = 'https://github.com/denissimon/SQLiteAdapter'
55
s.authors = { 'Denis Simon' => 'denis.v.simon@gmail.com' }
66
s.summary = 'A simple wrapper around SQLite3'

0 commit comments

Comments
 (0)