SQLite.Swift 中的一些用法

SQLite.Swift : https://github.com/stephencelis/SQLite.swift

 

        let filemgr = NSFileManager.defaultManager()

        let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

        let docsDir = dirPaths[0] as String

        var databasePath = docsDir.stringByAppendingPathComponent("dbname.db")

        let db = Database(databasePath)


        let mytable = db["tablename"]

        db.create(table: logs, ifNotExists: true){ t in

            t.column(Expression<Int64>("id"), primaryKey: .Autoincrement)

            t.column(Expression<String>("column1"), unique: false)

            t.column(Expression<String>("column2"))

            t.column(Expression<String>("column3"), unique: false)

        }

        //插入 , 返回插入的rowid
       if let insertId = logs.insert( Expression<String>("column1") <- "aaaaa",Expression<String>("column2") <- "bbbbb", Expression<String>("column3") <- "ccccc") {
            println("inserted id: \(insertId)")
        }
    

        //查询, 降序查询5条 "cloumn1"字段为“aaaaa”的条目
        let result = mytable.filter(Expression<String>("cloumn1") == "aaaaa").order(Expression<Int64>("id").desc) .limit(5)
        for log in result {
            pritln(log[Expression<String>("column2")])
        }

 

        

posted @ 2015-04-03 12:28  Rambot  阅读(2189)  评论(0编辑  收藏  举报