Created
July 6, 2015 14:49
-
-
Save mraaroncruz/0e95f1260d0ef480e3cb to your computer and use it in GitHub Desktop.
Swift SQLite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ActivityStore.swift | |
// Whacha Doin | |
// | |
// Created by Aaron Cruz on 6/21/15. | |
// Copyright (c) 2015 Aaron Cruz. All rights reserved. | |
// | |
import Foundation | |
import SQLite | |
class ActivityStore { | |
var db: Database! | |
var id: Expression<Int64>! | |
var name: Expression<String>! | |
var createdAt: Expression<NSDate>! | |
var activities: Query! | |
init(database: Database) { | |
self.db = database | |
createDB() | |
} | |
private func createDB() { | |
id = Expression<Int64>("id") | |
name = Expression<String>("name") | |
createdAt = Expression<NSDate>("created_at") | |
activities = db["activities"] | |
db.create(table: activities, ifNotExists: true) { t in | |
t.column(id, primaryKey: true) | |
t.column(name) | |
t.column(createdAt) | |
} | |
db.create(index: activities, on: name, ifNotExists: true) | |
db.create(index: activities, on: createdAt, ifNotExists: true) | |
} | |
// returns all activities | |
func all() -> [Activity] { | |
var models: [Activity] = [] | |
var activity: Activity | |
for a in activities.select(id, name, createdAt) | |
.order(createdAt.desc) { | |
activity = Activity(name: a[name], createdAt: a.get(createdAt)) | |
models.append(activity) | |
} | |
return models | |
} | |
// saves an activity in store | |
// returns true if successful | |
func save(activity: Activity) -> Bool { | |
let insert = activities.insert(name <- activity.name, createdAt <- NSDate()) | |
// I think this is how you can check for insertion | |
if insert.rowid != nil { | |
return true | |
} | |
return false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// Whacha Doin | |
// | |
// Created by Aaron Cruz on 6/20/15. | |
// Copyright (c) 2015 Aaron Cruz. All rights reserved. | |
// | |
import UIKit | |
let DB = DatabaseHelper.initializeDB("activities") | |
let activityStore = ActivityStore(database: DB) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DatabaseHelper.swift | |
// Whacha Doin | |
// | |
// Created by Aaron Cruz on 6/21/15. | |
// Copyright (c) 2015 Aaron Cruz. All rights reserved. | |
// | |
import Foundation | |
import SQLite | |
struct DatabaseHelper { | |
static func initializeDB(name: String) -> Database { | |
let path = NSSearchPathForDirectoriesInDomains( | |
.DocumentDirectory, .UserDomainMask, true | |
).first as! String | |
return Database("\(path)/\(name).sqlite3") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment