SQLiteSwift 0.1.2

SQLiteSwift 0.1.2

测试测试
语言语言 SwiftSwift
许可证 MIT
发布时间最后发布时间2016年5月
SPM支持 SPM

ohde-sg 维护。



  • ohde-sg

SQLiteSwift

SQLiteSwift 是一个使用 Swift 编写的库,它使您轻松处理 sqlite。

如何使用

步骤 1

  • 创建模型

    class User: SSMappable {
      static var table:String = "User" // Table Name
      var id:Int?                      // INTEGER Column
      var name:String?                 // TEXT Collumn
      var age:Int?
      var nickname:String?
      var isMan:Bool?                  // INTEGER Column
    
      required init(){
      }
    
      func dbMap(connector:SSConnector){
          // id column primarykey, autoincrement, notnull attribute
          id       <- connector["id", .PrimaryKey, .AutoIncrement, .NotNull]
          // name column unique attribute
          name     <- connector["name", .Unique]
          // age column check attribute
          age      <- connector["age", .Check("age>0")]
          // nickname column default attribute
          nickname <- connector["nickname", .Default("None")]
          // isMan column auto convert INTEGER(SQLite) <=> Bool(Swift)
          isMan    <- connector["isMan"]
      }
    }

步骤 2

  • 创建表

      let createTable:SSResult<User> = SQLiteConnection(filePath: dbFilePath).createTable() //make 'User' Table
      if createTable.result {
        print("correct!! make a table")
      }else {
        print("failed to make a table")
      }
  • INSERT

    let user = User()
    user.name = "takashi"
    user.age = 27
    user.nickname = "takayan"
    user.isMan = true
    
    let insert:SSResult<User> = SQLiteConnection(filePath: dbFilePath).insert(user) // insert user row
    
  • SELECT

    let table:SSTable<User> = SQLiteConnection(filePath: dbFilePath).table() // select User table
    for user in table.records {
      print(user.name,user.age,user.nickname, user.isMan)
    }
  • UPDATE

    let table:SSTable<User> = SQLiteConnection(filePath: dbFilePath).table()
    let model = table.records[0]
    model.age = 30
    model.isMan = false
    let update:SSResult<User> = SQLiteConnection(filePath: dbFilePath).insert(user) // update user row
  • DELETE

    let table:SSResult<User> = SQLiteConnection(filePath: dbFilePath).table()
    let model = table.records[0]
    let delete:SSResult<User> = SQLiteConnection(filePath: dbFilePath).delete(model) // delete user row
  • IsExistTable

    let isExistTable:SSResult<User> = SQLiteConnection(filePath: dbFilePath).isExistTable() // is Exist User table
    if isExistTable.result {
      print("Table is exist")
    }
  • 删除表

    let deleteTable:SSResult<User> = SQLiteConnection(filePath: dbFilePath).deleteTable() // delete User table
    if deleteTable.result {
      print("delete table complete!!")
    }
  • 查询

    let query = "SELECT name, age FROM User WHERE age>? AND age<?;"
    let values = [21,30]
    let result:SSTable<User> = SQLiteConnection(filePath: dbFilePath).query(query, params: values)

更多

  • 事务提交

    let connect = SQLiteConnection(filePath: dbFilePath)
    connect.beginTransaction()
    // INSERT,DELETE,UPDATE using connect
    connect.commit()