2016-07-27 109 views
0

我正在使用sqlite.swift進行數據庫創建。我在這個鏈接http://masteringswift.blogspot.in/2015/09/create-data-access-layer-with.html的幫助下創建了一個班級。我想在球員表格引用的teamID(column)上創建一個外鍵作爲team table teamID(column)。如何使用數據模型在sqlite.swift中創建外鍵

這裏是球隊和球員表中的輔助類:

class TeamDataHelper: DataHelperProtocol { 
static let TABLE_NAME = "Teams" 

static let table = Table(TABLE_NAME) 
static let teamId = Expression<Int64>("teamid") 
static let city = Expression<String>("city") 
static let nickName = Expression<String>("nickname") 
static let abbreviation = Expression<String>("abbreviation") 


typealias T = Team 

static func createTable() throws { 
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else { 
     throw DataAccessError.Datastore_Connection_Error 
    } 
    do { 
     let _ = try DB.run(table.create(ifNotExists: true) {t in 
      t.column(teamId, primaryKey: true) 
      t.column(city) 
      t.column(nickName) 
      t.column(abbreviation) 
      }) 

    } catch _ { 
     // Error throw if table already exists 
    } 

} 

static func insert(item: T) throws -> Int64 { 
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else { 
     throw DataAccessError.Datastore_Connection_Error 
    } 
    if (item.city != nil && item.nickName != nil && item.abbreviation != nil) { 
     let insert = table.insert(city <- item.city!, nickName <- item.nickName!, abbreviation <- item.abbreviation!) 
     do { 
      let rowId = try DB.run(insert) 
      guard rowId > 0 else { 
       throw DataAccessError.Insert_Error 
      } 
      return rowId 
     } catch _ { 
      throw DataAccessError.Insert_Error 
     } 
    } 
    throw DataAccessError.Nil_In_Data 

} 

static func delete (item: T) throws -> Void { 
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else { 
     throw DataAccessError.Datastore_Connection_Error 
    } 
    if let id = item.teamId { 
     let query = table.filter(teamId == id) 
     do { 
      let tmp = try DB.run(query.delete()) 
      guard tmp == 1 else { 
       throw DataAccessError.Delete_Error 
      } 
     } catch _ { 
      throw DataAccessError.Delete_Error 
     } 
    } 
} 

static func find(id: Int64) throws -> T? { 
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else { 
     throw DataAccessError.Datastore_Connection_Error 
    } 
    let query = table.filter(teamId == id) 
    let items = try DB.prepare(query) 
    for item in items { 
     return Team(teamId: item[teamId] , city: item[city], nickName: item[nickName], abbreviation: item[abbreviation]) 
    } 

    return nil 

} 

static func findAll() throws -> [T]? { 
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else { 
     throw DataAccessError.Datastore_Connection_Error 
    } 
    var retArray = [T]() 
    let items = try DB.prepare(table) 
    for item in items { 
     retArray.append(Team(teamId: item[teamId], city: item[city], nickName: item[nickName], abbreviation: item[abbreviation])) 
    } 

    return retArray 

} 
} 



class PlayerDataHelper: DataHelperProtocol { 
static let TABLE_NAME = "Players" 

static let playerId = Expression<Int64>("playerid") 
static let firstName = Expression<String>("firstName") 
static let lastName = Expression<String>("lastName") 
static let number = Expression<Int>("number") 
static let teamId = Expression<Int64>("teamid") 
static let position = Expression<String>("position") 


static let table = Table(TABLE_NAME) 

typealias T = Player 

static func createTable() throws { 
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else { 
     throw DataAccessError.Datastore_Connection_Error 
    } 
    do { 
     _ = try DB.run(table.create(ifNotExists: true) {t in 

      t.column(playerId, primaryKey: true) 
      t.column(firstName) 
      t.column(lastName) 
      t.column(number) 
      t.column(teamId) 
      t.column(position) 


      }) 
    } catch _ { 
     // Error thrown when table exists 
    } 
} 

static func insert(item: T) throws -> Int64 { 
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else { 
     throw DataAccessError.Datastore_Connection_Error 
    } 
    if (item.firstName != nil && item.lastName != nil && item.teamId != nil && item.position != nil) { 
     let insert = table.insert(firstName <- item.firstName!, number <- item.number!, lastName <- item.lastName!, teamId <- item.teamId!, position <- item.position!.rawValue) 
     do { 
      let rowId = try DB.run(insert) 
      guard rowId >= 0 else { 
       throw DataAccessError.Insert_Error 
      } 
      return rowId 
     } catch _ { 
      throw DataAccessError.Insert_Error 
     } 
    } 
    throw DataAccessError.Nil_In_Data 
} 

static func delete (item: T) throws -> Void { 
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else { 
     throw DataAccessError.Datastore_Connection_Error 
    } 
    if let id = item.playerId { 
     let query = table.filter(playerId == id) 
     do { 
      let tmp = try DB.run(query.delete()) 
      guard tmp == 1 else { 
       throw DataAccessError.Delete_Error 
      } 
     } catch _ { 
      throw DataAccessError.Delete_Error 
     } 
    } 

} 

static func find(id: Int64) throws -> T? { 
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else { 
     throw DataAccessError.Datastore_Connection_Error 
    } 
    let query = table.filter(playerId == id) 
    let items = try DB.prepare(query) 
    for item in items { 
     return Player(playerId: item[playerId], firstName: item[firstName], lastName: item[lastName], number: item[number], teamId: item[teamId], position: Positions(rawValue: item[position])) 
    } 

    return nil 

} 

static func findAll() throws -> [T]? { 
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else { 
     throw DataAccessError.Datastore_Connection_Error 
    } 
    var retArray = [T]() 
    let items = try DB.prepare(table) 
    for item in items { 
     retArray.append(Player(playerId: item[playerId], firstName: item[firstName], lastName: item[lastName], number: item[number], teamId: item[teamId], position: Positions(rawValue: item[position]))) 
    } 

    return retArray 
} 
} 

在上面播放表我想創建作爲參考團隊表teamID爲teamID外鍵。如果我正在創建諸如「t.foreignKey(teamID,on:TeamDataHelper.teamID)」的外鍵,它會給出錯誤。

任何人都可以建議我任何幫助。

+0

有什麼錯誤? – polku

+0

@polku無法使用類型爲'(表達式,表達式) –

+0

的任何人建議我來調用「foreignkey」。 –

回答

0

1.檢查您的陳述。對於斯威夫特4(和Xcode中9),它應該是:

t.foreignKey(teamId, references: teamTable, teamId, delete: .setNull) 
// FOREIGN KEY("teamid") REFERENCES "Team"("teamid") ON DELETE SET NULL 

把它放在DB.run在CREATETABLE()FUNC在PlayerDataHelper類

2.You還需要下面的代碼在你的PlayerDataHelper類

teamTable = Table("Team") 

參考:SQLite.swift ~> 0.11.4 GitHub Documentation

相關問題